为什么IE不添加< option>我的dropDown? [英] Why does IE not add <option> to my dropDown?

查看:86
本文介绍了为什么IE不添加< option>我的dropDown?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用IE运行示例脚本

var $select = $('#select');
var $button = $('#button');

$button.click(function() {
    var $option = $('<option />');
    $option.text('hello');
    $option.appendTo($select);
});

var $tabs = $('#tabs');
$tabs.tabs();

这很简单:当点击按钮时,选项应该添加到我的下拉列表中。这很好用 - IE中的基本功能。

It's pretty straight forward: when clicking on the button, an option should be added to my dropdown. This works great - the basic fct in IE either.

我的问题:

只需打开dropDown,然后再关闭它。现在切换到选项卡按钮并按下按钮。现在切换到选项卡选择 - 一个新的选项应该可用。

这对除了IE之外的每个浏览器都很有效...(有时IE浏览器在几个制表符开关之后)

My problem:
just "open" the dropDown, and "close" it again. Now switch to tab "button" and hit the button. Now switch to tab "select" - a new option should be available.
This works great on every browser except IE ... (sometimes IE messes up after several tab-switches)

如何修复我的脚本?

推荐答案

添加选项框的选项要比添加选项 DOM元素(实例):

There's a much simpler, and more cross-browser friendly, way to add options to select boxes than adding option DOM elements (live example):

$button.click(function() {
    var option = new Option('hello');
    $select[0].options.add(option);
});

也许这对你来说更可靠。 (请注意,它是添加 推送选项 上的对象选择元素实际上不是数组。)

Perhaps that would work more reliably for you. (Note that it's add, not push. The options object on select elements isn't really an array.)

而不是 options.add 你也可以这样做:

Instead of options.add you can also do:

$button.click(function() {
    var options = $select[0].options,
        option  = new Option('hello');
    options[options.length] = option;
});

...它也以类似数组的方式添加到最后。但添加是可靠的跨浏览器。

...which also adds to the end, in an array-like way. But add is reliable cross-browser.

这篇关于为什么IE不添加&lt; option&gt;我的dropDown?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆