在Select元素的Option元素中获取自定义属性

时间:2012-01-24 14:55:37

标签: javascript html internet-explorer firefox google-chrome

  

可能重复:
  Browser Independence issue Code Working for IE but not for Firefox and Chrome

我正在尝试支持一些旧代码,并且我在html中的Select元素中有一个选项元素:

<select onChange="selectURL(this)" style="font-family:Arial; font-size:12;" id="select1" name="select1">

<option ThisCourse="80921" TITLE="- _ ~ : /  =  ’ ; . , ?  ) (.2\" CLASS="" ID="a368890534077645903f263b5ddc105f" VALUE="/Eclass/CourseFrames.asp?vcCourseGUID=a368890534077645903f263b5ddc105f&amp;vcCourseName=%2D+%5F+%7E+%3A+%2F++%3D++%92+%3B+%2E+%2C+%3F++%29+%28%2E2%5C&amp;vcCourseID=80921&amp;NewWin=0&amp;language=">- _ ~ : /  =  ’ ; . , ?  ) (.2\</option>

该网站上运行了一些javascript,它正在尝试这样做:

for (i=0; i<SelectElement.options.length;i++)
if (SelectElement.options[i].ThisCourse == CourseID)
SelectElement.selectedIndex = i

在IE中,这很好用 - 它可以设法进入ThisCourse。但是,Firefox和Chrome不了解SelectElement.options [i] .ThisCourse

我猜是有一些现代的方式来获得“ThisCourse”,但我不知道如何。

2 个答案:

答案 0 :(得分:1)

尝试使用getAttribute方法而不是访问属性,就像这样...

if (SelectElement.options[i].getAttribute('ThisCourse') == CourseID)

IE通常会对属性值和属性进行处理,但它们是不同的。

答案 1 :(得分:0)

尝试:

SelectElement.options[i].getAttribute("ThisCourse");

您应该转向使用HTML5“data-”属性。

http://html5doctor.com/html5-custom-data-attributes/

相关问题