【问题标题】:HTML - MOOTOOLS- Add option to select box in ie6HTML - MOOTOOLS- 添加选项以在 ie6 中选择框
【发布时间】:2010-09-29 04:46:17
【问题描述】:
<select id="selectId">
 <option>Please select product ...</option>
</select>

我在firefox上试了试

$('selectId').innertHTML = '<option>Test test</option>'

但在 ie 上,它不起作用,如何通过字符串选项添加选项,如上面在 ie 中

【问题讨论】:

  • 您确定这不是拼写错误吗? $('selectId').innertHTML = ''
  • 哦,我用innerHTML试试,结果一样,就是不行~.~

标签: mootools


【解决方案1】:

改用Element 类:

new Element('option', {
    text: 'test option'
}).inject($('selectId'));​

示例:http://www.jsfiddle.net/EJH5b/

【讨论】:

  • +1。阅读文档 ppl :) 我会将value: nnn 添加到正在传递的对象中。您也可以按照您的预期传递selected 属性
【解决方案2】:

如果你打算使用 Mootools,那么你应该真正使用 Mootools 方法,而不是在它和 vanilla Javascript 之间切换。这样做的一个好处是 Mootools 已经在为您处理浏览器的不一致问题。因此,如果您忽略它的方法,您将不得不自己处理它们。

要以 Mootools 方式访问元素的属性,您可以在任何元素上使用 setget 方法。美元 ($) 函数返回一个元素,因此您可以链式设置并获取该元素。

//returns selectId's HTML
var foo = $('selectId').get('html'); 

//Sets the HTML of selectId to <option>Test test</option>
$('selectId').set('html', '<option>Test test</option>'); //

在这种情况下,你只需要使用 set。

您应该注意的一件事是,它不会向选择框添加选项,而是将选择框内的所有内容替换为选项。因此,如果您想使用它向选择框添加多个选项,则它不会起作用,因为您每次都在重置 HTML。拥有一个只有一个选项的选择框没有多大意义,所以我假设您正在尝试附加一个选项,而不是用一个选项替换所有内容。在这种情况下,请执行以下操作:

//get the current options selectId's options
var options = $('selectId').get('html'); 

//Add your own option onto the end
options = options + '<option>Test test</option>';

//Set the HTML of selectId to whatever is stored in options
$('selectId').set('html', options);

【讨论】:

  • 在这种情况下,您是否尝试过使用正确的选项创建一个全新的选择框,删除旧的选择然后插入新的选择?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-15
  • 1970-01-01
相关资源
最近更新 更多