为什么IE不添加< option>我的dropDown? [英] Why does IE not add <option> to my 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不添加< option>我的dropDown?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!