jQuery 如何使用jQuery一次性隐藏多个选择器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8217853/
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
How to hide multiple selectors as once with jQuery
提问by klewis
How come this..
这怎么..
$("div.toggle1").hide();
$("div.toggle3").hide();
$("div.toggle4").hide();
$("div.toggle5").hide();
is not equvalent to this...
不等于这个...
$('#container div').not('.toggle2').hide();
that occurs on a click event, but it doesn't work the same as manually typing out multiple hide() tags. I'm just trying to reduce the hide() tag usage for every div I keep adding on within my parent #container div.
这发生在点击事件上,但它与手动输入多个 hide() 标签的工作方式不同。我只是想减少我在父级 #container div 中不断添加的每个 div 的 hide() 标签使用。
回答by Naftali aka Neal
$("div.toggle1, div.toggle3, div.toggle4, div.toggle5").hide();
Or just give every DOM element that you will hide the same class and you can just do:
或者只是给每个 DOM 元素,你将隐藏同一个类,你可以这样做:
$('.hideClass').hide();
回答by ShankarSangoli
You can easily control it through css. Add a hidden class to parent container which will have a display:none
style in it. If you don't want display:none
for div with class toggle2
then override the style for this element. In this way you dont have to call hide on all the containers or select all the containers and call hide method.
您可以通过css轻松控制它。向父容器添加一个隐藏类,其中将具有display:none
样式。如果您不想要display:none
带有类的 div,toggle2
则覆盖此元素的样式。通过这种方式,您不必对所有容器调用 hide 或选择所有容器并调用 hide 方法。
Try this
尝试这个
.hidden{
display:none;
}
.hidden .toggle2{
display:block;
}
//This will add hidden class to the container which will
//ultimately hide all the inner divs except div with class toggle2
$('#container').addClass('hidden');