为什么IE不添加<选项>到我的下拉菜单?选项>
我正在使用 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 中的基本功能。
我的问题:
只需“打开”下拉菜单,然后再次“关闭”它。现在切换到“按钮”选项卡并点击按钮。现在切换到“选择”选项卡 - 应该会出现一个新的选项
。
这在除 IE 之外的所有浏览器上都非常有效...(有时 IE 在几次选项卡切换后会出现混乱)
我该如何修复我的脚本?
I'm running this sample script with IE:
var $select = $('#select');
var $button = $('#button');
$button.click(function() {
var $option = $('<option />');
$option.text('hello');
$option.appendTo($select);
});
var $tabs = $('#tabs');
$tabs.tabs();
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.
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)
How can I fix my script?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
与添加
option
DOM 元素 (实例):也许这对你来说更可靠。 (请注意,它是
add
,不是push
。select
元素上的options
对象并不是真正的数组。)除了
options.add
之外,您还可以执行以下操作:...这也会以类似数组的方式添加到末尾。但
add
是可靠的跨浏览器。There's a much simpler, and more cross-browser friendly, way to add options to select boxes than adding
option
DOM elements (live example):Perhaps that would work more reliably for you. (Note that it's
add
, notpush
. Theoptions
object onselect
elements isn't really an array.)Instead of
options.add
you can also do:...which also adds to the end, in an array-like way. But
add
is reliable cross-browser.