jquery,id 中类的选择器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17417614/
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
jquery, selector for class within id
提问by user984003
Below, how should I select the elements that contain the class my_class
within the element with id = "my_id"
?
下面,我应该如何选择包含元素my_class
内的类的元素id = "my_id"
?
Note that the element may also have another class, which I am not selecting for.
请注意,该元素可能还有另一个类,我没有选择它。
<div id = "my_id">
<span class = "my_class hidden">hi</span>
<span class = "my_class">hello</span>
</div>
was trying
尝试着
$("#my_id [class*=my_class ]")
回答by Arun P Johny
回答by doppelgreener
Just use the plain ol' class selector.
只需使用普通的 ol' 类选择器。
$('#my_id .my_class')
It doesn't matter if the element also has other classes. It has the .my_classclass, and it's somewhere inside #my_id, so it will match that selector.
元素是否也有其他类并不重要。它有.my_class类,它位于#my_id内的某个地方,因此它将匹配该选择器。
Regarding performance
关于性能
According to the jQuery selector performance documentation, it's faster to use the two selectors separately, like this:
根据jQuery 选择器性能文档,单独使用两个选择器会更快,如下所示:
$('#my_id').find('.my_class')
Here's the relevant part of the documentation:
这是文档的相关部分:
ID-Based Selectors
// Fast: $( "#container div.robotarm" ); // Super-fast: $( "#container" ).find( "div.robotarm" );
The
.find()
approach is faster because the first selection is handled without going through the Sizzle selector engine – ID-only selections are handled usingdocument.getElementById()
, which is extremely fast because it is native to the browser.
基于 ID 的选择器
// Fast: $( "#container div.robotarm" ); // Super-fast: $( "#container" ).find( "div.robotarm" );
这种
.find()
方法更快,因为第一个选择是在不通过 Sizzle 选择器引擎的情况下处理的——只有 ID 的选择是使用 处理的document.getElementById()
,这非常快,因为它是浏览器的本机。
Selecting by IDor by classalone (among other things) invokes browser-supplied functions like document.getElementById()
which are quite rapid, whereas using a descendent selectorinvokes the Sizzle engine as mentioned which, although fast, is slower than the suggested alternative.
选择由ID或由类单独(除其他外)调用浏览器提供的功能,如document.getElementById()
它们是相当快,而使用一个后代选择调用灒发动机如所提到的,其尽管快,比建议的替代慢。
回答by Niks Jain
Always use
一直使用
//Super Fast
$('#my_id').find('.my_class');
instead of
代替
// Fast:
$('#my_id .my_class');
Have look at JQuery Performance Rules.
Also at Jquery Doc
同样在Jquery Doc
回答by Pasha Oleynik
Also $( "#container" ).find( "div.robotarm" );
is equal to: $( "div.robotarm", "#container" )
也$( "#container" ).find( "div.robotarm" );
等于:$( "div.robotarm", "#container" )
回答by Thirumalai murugan
I think your asking to select only <span class = "my_class">hello</span>
this element, You have do like this, If I am understand your question correctly this is the answer,
我认为您要求仅选择<span class = "my_class">hello</span>
此元素,您已经这样做了,如果我正确理解您的问题,这就是答案,
$("#my_id [class='my_class']").addClass('test');