jQuery“可见”并不适用于所有浏览器,但适用于 Firefox

发布于 2024-12-10 18:09:37 字数 709 浏览 1 评论 0原文

我在这里制作了一个非常简单的小提琴,您可以在不同的浏览器中查看它。

它仅适用于 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 技术交流群。

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

发布评论

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

评论(2

送你一个梦 2024-12-17 18:09:37

这不是 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.

美羊羊 2024-12-17 18:09:37

事实上, :hidden:visible 不适用于

您可以尝试使用 disabled="已禁用” 请参阅:

http://jsfiddle.net/sZR2f/7/

Indeed, :hidden and :visible don't work on <option>'s

You could try to use disabled="disabled" see:

http://jsfiddle.net/sZR2f/7/

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