简体   繁体   English

jQuery是否会选择隐藏元素

[英]Will jQuery select hidden elements

If I call .hide() on an element, will/can jQuery select it in a normal dom selector. 如果我在元素上调用.hide() ,jQuery将/可以在常规dom选择器中选择它。

If jQuery does normally select hidden elements, what is the proper way to select only visible elements. 如果jQuery通常选择隐藏元素,那么仅选择可见元素的正确方法是什么。 Can I use a css selector, or is there a more valid way of doing this? 我可以使用CSS选择器,还是有一种更有效的方法?

Yes. 是。 The hide function only stores the current value of the display css property of your element, then set it to none . hide函数仅存储元素的display css属性的当前值,然后将其设置为none So the dom selectors will not be affected by it unless they try to match elements with a particular display css value. 因此,除非dom选择器尝试匹配具有特定display css值的元素,否则它不会受到它的影响。

Check it here . 在这里检查。

Have a look at the jQuery hide function documentation . 看一下jQuery hide函数文档

是的,它将计算隐藏的元素。

Yes, it just adds a display:none style to the element... .remove() on the other hand will not show up in counts. 是的,它只是添加了display:none样式。...另一方面,.remove()不会显示在计数中。 But that completely gets rid of it, and unless you store the value somewhere it is not retrievable. 但这完全摆脱了它,除非您将值存储在某个地方,否则它是不可检索的。

What I'm assuming you want to do is to count the visible items. 我假设您想做的是计算可见项。 I would instead do the following: 我将改为执行以下操作:

$('.element').addClass('hide');

var count_of_visible_items = $('.element:not(".hide")').length;
console.log(count_of_visible_items);

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

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