在jQuery中,我如何选择一个隐藏的元素? [英] In jQuery, how can I select a hidden element?
问题描述
如何选择< 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 =nofollow noreferrer> :hidden
选择器
请注意,按ID选择元素的正确方法是:
$ #test1);
做你所做的事情是使jQuery做不必要的解析,并且慢得多。
如果只有在隐藏时选择#test1
,您可以这样做:
$(#test1:hidden);
如果您要选择< span> $ c $隐藏在
#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屋!