jQuery“可见”并不适用于所有浏览器,但适用于 Firefox
我在这里制作了一个非常简单的小提琴,您可以在不同的浏览器中查看它。
它仅适用于 Firefox。换句话说,似乎 $('#select-tag-id option:visible')
在其他浏览器中不起作用。怎么了?这是一个 jQuery 错误吗?
代码是:
<select id='items'>
<option value='1' style='display: none;'>One</option>
<option value='1' style='display: block;'>Two</option>
<option value='1' style='display: block;'>Three</option>
<option value='1' style='display: none;'>Four</option>
</select>
JavaScript(jQuery 代码)是:
$(function(){
alert($('#items option:visible').length);
});
I've made a very simple fiddle here, and you can check it out in different browsers.
It only works in Firefox. In other words, seems that $('#select-tag-id option:visible')
doesn't work in other browsers. What's wrong? Is it a jQuery bug?
The code is:
<select id='items'>
<option value='1' style='display: none;'>One</option>
<option value='1' style='display: block;'>Two</option>
<option value='1' style='display: block;'>Three</option>
<option value='1' style='display: none;'>Four</option>
</select>
and the JavaScript (jQuery code) is:
$(function(){
alert($('#items option:visible').length);
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不是 jQuery 错误 - 只是(又一个)浏览器差异。
IE 不允许您在选项元素上设置
display:none
(style.display='none' 不适用于选项标签)。如果您在 FF 和 IE 中查看 fiddle,您会发现
解决方案可能是实际移除这些元件并在需要时进行更换。
It's not a jQuery bug - just (yet another) browser difference.
IE won't let you set
display:none
on option elements (style.display='none' doesnt work on option tags).If you look at your fiddle in both FF and IE, you'll see that the
<select>
still contains all four elements in IE, but only two in FF, regardless of jQuery being present.The solution would probably be to actually remove the elements and replace when needed.
事实上,
:hidden
和:visible
不适用于您可以尝试使用
disabled="已禁用”
请参阅:http://jsfiddle.net/sZR2f/7/
Indeed,
:hidden
and:visible
don't work on<option>
'sYou could try to use
disabled="disabled"
see:http://jsfiddle.net/sZR2f/7/