需要使用 jQuery.find 来查找具有特定样式的元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1562634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Need to use jQuery.find to find element with specific style
提问by Steven
Right... I need to find all < ul style="display: block;"> elements, so that I can set it do display:none.
对...我需要找到所有 <ul style="display:block;"> 元素,以便我可以将其设置为 display:none。
I think I'm on the right path here... but not quite there:
我认为我在这里走在正确的道路上......但不完全在那里:
jQuery('#adminMenu li').find("ul").css('display');
For advance users: how can I find and change the style with one line?
对于高级用户:如何在一行中查找和更改样式?
回答by cletus
That's tricky as stated but is solvable other ways. The easiest is:
如前所述,这很棘手,但可以通过其他方式解决。最简单的是:
$("#adminMenu li ul:visible").hide();
assuming items are either hidden or not. Of course, considering you want to hide them all why not just:
假设项目隐藏或不隐藏。当然,考虑到你想隐藏它们,为什么不只是:
$("#adminMenu li ul").hide();
Try and avoid changing CSS style directly. It's problematic. It's hard to reverse and as you're discovering hard to search for. Use a class instead:
尽量避免直接更改 CSS 样式。这是有问题的。很难逆转,因为你发现很难搜索。改用类:
#adminMenu li ul { display: none; }
ul.block { display: block; }
with:
和:
$("#adminMenu li ul").removeClass("block");
or
或者
$("#adminMenu li ul.block").removeClass("block");
回答by Corey Downie
You may be able to use the attribute selectors, and the 'contains' option
您可以使用属性选择器和“包含”选项
$('#adminMenu li ul[style*="display:block"]').hide()
This essentially says 'any ul who's style attribute contains the text display:block'
这实质上是说“任何 ul 的样式属性都包含文本 display:block”
回答by asankasri
You can use filter. (http://api.jquery.com/filter/)
您可以使用过滤器。( http://api.jquery.com/filter/)
So you can do as below.
所以你可以做如下。
$('ul').filter(function() {
return $(this).css('display') == 'block';
});
回答by Russ Cam
$('#adminMenu li').find("ul:visible").css('display', 'none');
or
或者
$('#adminMenu li').find("ul:visible").hide();
or
或者
$('#adminMenu li ul:visible').hide();
to name a few ways
列举几种方法
回答by Corey Ballou
You coud use the fadeOut() or slideUp() methods for a visual effect:
您可以使用fadeOut() 或slideUp() 方法来获得视觉效果:
jQuery('#adminMenu li').find("ul").fadeOut('fast');
jQuery('#adminMenu li').find("ul").slideUp('fast');
回答by powtac
$('#adminMenu li ul').hide();
回答by Seb
Don't know how to do it in one line, but here's how you can do it in a couple more:
不知道如何在一行中完成,但您可以通过以下方式完成更多操作:
jQuery('#adminMenu li').find("ul").each(function (){
if($(this).css("display") == "block"){
// do something
}
});
If what you want is to handle all visible elements (instead of only the display: block
ones), you could try the :visible
selector instead.
如果您想要处理所有可见元素(而不仅仅是display: block
那些),您可以尝试使用:visible
选择器。