在jQuery中,我如何选择一个隐藏的元素? [英] In jQuery, how can I select a hidden element?

查看:332
本文介绍了在jQuery中,我如何选择一个隐藏的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何选择< span> 其中显示设置为 none 在以下代码中?

 < p id =p1> 
< span id =test1style =display:none> test1< / span>
< span id =test2> test2< / span>
< / p>



我可以选择< span> 通过使用 $(span [id ='test1']),其ID为test1,但是当我使用 $ (span [style ='display:none'])



有没有方法一次获取这个元素? / p>

非常感谢。

解决方案

href =http://docs.jquery.com/Selectors/hidden =noreferrer> :hidden 选择器



请注意,通过ID选择元素的正确方法是:

  $ #test1); 

按照你所做的方式使jQuery做不必要的解析, >

如果只有在隐藏时选择#test1 ,您可以这样做:

  $(#test1:hidden); 

如果您要选择所有< span> #p1 下的元素c:

  $(span:hidden,#p1); 

如注释中所述,此选择器的相反是 :visible 选择器:

  $(span:visible,#p1); 

然后选择任何可见的< span> 元素#p1 中的元素。


How can I select the <span> where display is set to none in the below code?

<p id="p1">
<span id="test1" style="display:none">test1</span> 
<span id="test2" >test2</span> 
</p>

I can select the <span> whose ID is "test1" by using $("span[id='test1']"), but it does not work when I use $("span[style='display:none']").

Is there any method to get this element at a time?

Thanks a lot.

解决方案

You are looking for the :hidden selector

Please note that the proper way of selecting an element by ID is simply:

$("#test1");

Doing it the way you are doing is making jQuery do unnecessary parsing and is much slower.

If you want to select #test1 only if it is hidden, you do this:

$("#test1:hidden");

If you wanted to select all <span> elements that are hidden under #p1, you do this:

$("span:hidden", "#p1");

As noted in the comments, the opposite of this selector is the :visible selector:

$("span:visible", "#p1");

Would then select any visible <span> elements in the element #p1.

这篇关于在jQuery中,我如何选择一个隐藏的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆