附加选择选项 - IE 问题/解决方案导致其他浏览器问题

发布于 2024-09-27 16:44:25 字数 247 浏览 0 评论 0原文

我有一个

当然,因为 IE 不能使用innerHTML,所以我必须将此模板附加到

I have a <select> that depending on previous options is populated by a var with a bunch of <option> values.

Naturally, because IE doesn't work with innerHTML I have to append this template to the <select> which now works great in IE. HOWEVER I now need a way to clear out the select options from the previous search and in FF stop it from dropping down to the last <option> in the list.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只需使用innerHTML重写整个选择块,这总是有效的。

Just rewrite the entire select block using innerHTML, that always works.

‘画卷フ 2024-10-04 16:44:25

while( select_control.length > 0 )
     select_control.options[0] = null

如果您执行select_control.length = 0,某些浏览器会清除列表,但我发现这不可靠。

插入选项的万无一失的方法:

var new_option = new Option(text, value)
try {
    select_control.add(new_option, select_control.options[0])
} catch(e) {
    select_control.add(new_option, 0)
}

0 是您之前想要的项目的索引。要将其添加到末尾,请执行以下操作:

select_control.options[select_control.length] = new_option

如果您想通过指定现有选项的索引来替换特定项目,这也将起作用。

Foolproof way of clearing out the options from a <select>:

while( select_control.length > 0 )
     select_control.options[0] = null

Some browsers will clear the list if you do select_control.length = 0 but I've found this unreliable.

Foolproof way of inserting option:

var new_option = new Option(text, value)
try {
    select_control.add(new_option, select_control.options[0])
} catch(e) {
    select_control.add(new_option, 0)
}

The 0 is the index of the item you want your item before. To add it to the end, do this instead:

select_control.options[select_control.length] = new_option

This will also work if you want to replace a specific item, by specifying the index of an existing option.

单身狗的梦 2024-10-04 16:44:25

使用 YUI3 良好:

    node.setContent(template); //removes old children, sets new template as content.
    node.set('selectedIndex', 0); //forces FF to select the first one

使用正确的方法进行编辑。

Well using YUI3:

    node.setContent(template); //removes old children, sets new template as content.
    node.set('selectedIndex', 0); //forces FF to select the first one

Edited with correct methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文