简体   繁体   English

如何在jquery中隐藏所有div

[英]how to hide all divs in jquery

I have several divs: 我有几个div:

<div id="div-1"></div>
<div id="div-2"></div>
<div id="div-3"></div>
<div id="div-4"></div>

How can I hide them all with jquery. 如何用jquery隐藏它们。 I used $('#div').hide(); 我用$('#div').hide(); and did not work. 并没有奏效。

You are using an id in your selector. 您在选择器中使用了id。 Simply use: 只需使用:

$('div').hide();

However that will hide literally all divs. 然而,这将隐藏所有divs。 How about you hide only divs that have an id in the form of div-x ? 你怎么样只隐藏div-x形式的id?

$('div[id^="div-"]').hide();

This will only hide the divs you mentioned without hiding other divs (which might be problematic). 这只会隐藏你提到的div而不会隐藏其他div(这可能会有问题)。

for more detail you can read this : Element Selector (“element”) 有关更多详细信息,请参阅: Element Selector (“element”)

this will do : $('div').hide(); 这样做: $('div').hide();

there is no need of # sign which is for the id selector for jquery , if you want to hide element just write the name of element will do your task thats called as "element selector". 对于jquery的id选择器,不需要#sign,如果要隐藏元素,只需写入元素的名称就可以完成你的任务,称为“元素选择器”。

Take out the hash and just do $('div').hide(); 取出哈希值,然后执行$('div').hide(); because right now you are hiding all elements with an id of "div" 因为现在你隐藏了所有id"div"元素

The problem is you are specifying an id in your selector. 问题是你在选择器中指定了一个id Use this instead: 请改用:

$('div').hide();

jQuery uses CSS-selectors, so this hides all divs: jQuery使用CSS选择器,所以这隐藏了所有div:

$('div').hide();

However, if you want to hide the divs whose id begins with "div", as in your example, do this: 但是,如果要隐藏id以“div”开头的div,如示例所示,请执行以下操作:

$('div[id^="div"]').hide();

Assign a class to all the divs you want to hide and then do sth like 将类分配给要隐藏的所有div,然后执行类似的操作

 $('.hider').hide()

This would hide all the divs with that class hider. 这将隐藏所有具有该类隐藏的div。 Then you can do whatever you want on some of the divs 然后你可以在一些div上做任何你想做的事

$('div').hide(); $( '格')隐藏()。 should work 应该管用

$('#div') looks for id="div" rather than looking for all divs. $('#div')查找id =“div”而不是查找所有div。

$('#div').hide();

does not work because you are lookin for something with ID = "div" and you have set your id's to "div-1" etc. 不起作用,因为你正在寻找ID =“div”的东西,你已经将你的id设置为“div-1”等。

Instead try 而是试试

  $('#div-1').hide();
  $('#div-2').hide();

etc 等等

That will hide the specific div's mentioned. 这将隐藏所提到的特定div。

If you really want to hide all the divs on your page then 如果你真的想隐藏页面上的所有div,那么

  $('div').hide();

#div the element who's id is div , if you want to hide every div on the page then the selector that you want is just div (use $('div').hide() ). #div谁是元素iddiv ,如果你想隐藏的每个div在页面上,然后要选择就是div (使用$('div').hide()

I don't think that is what you really want though, you almost certainly do not actually want to hide every div on the page. 我不认为这是你真正想要的,你几乎肯定不想隐藏页面上的每个 div。 You seem to be trying to hide several specific div s in one go. 你似乎试图一次性隐藏几个特定的div The way to do that is to separate the id 's with a comma: $('#div-1,#div-2,#div-3,#div-4').hide() . 这样做的方法是用逗号分隔id$('#div-1,#div-2,#div-3,#div-4').hide()

Alternately a better way to do it is to add a class to those div s in case you want to change the number of div s. 或者更好的方法是在你想要改变div的数量的情况下为这些div添加一个class

So to hide: 所以隐藏:

<div id="div-1" class="foo"></div>
<div id="div-2" class="foo"></div>
<div id="div-3" class="foo"></div>
<div id="div-4" class="foo"></div>

You would use $('.foo').hide() . 你会使用$('.foo').hide()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM