jQuery使用未知路径按类选择子元素

时间:2021-07-27 20:32:15

I am trying to figure out the syntax for selecting a nth-child of an element by its class, however I don't know the exact path to the element. I can't do $('parent > child > grandchild > hereIam');

我正在试图找出在它的类中选择元素的nth-child的语法,但是我不知道元素的确切路径。我不能做$('parent > child >, grandchild > hereIam');

So basically I need to be able to say

所以基本上我需要说

$('#thisElement').AllRelativesWithClass('.classToSelect')

How exactly do I do that?

我该怎么做呢?

4 个解决方案

#1


34  

According to this documentation, the find method will search down through the tree of elements until it finds the element in the selector parameters. So $(parentSelector).find(childSelector) is the fastest and most efficient way to do this.

根据本文档,find方法将在元素树中搜索,直到在选择器参数中找到元素。所以$(parentSelector).find(childSelector)是最快速、最有效的方法。

#2


16  

$('#thisElement').find('.classToSelect') will find any descendents of #thisElement with class classToSelect.

find('.classToSelect')将查找带有类classToSelect的#thisElement的任何后代。

#3


9  

This should do the trick:

这应该能达到目的:

$('#thisElement').find('.classToSelect')

#4


5  

Try this

试试这个

$('#thisElement .classToSelect').each(function(i){
         // do stuff
});

Hope it will help

希望它能帮助

#1


34  

According to this documentation, the find method will search down through the tree of elements until it finds the element in the selector parameters. So $(parentSelector).find(childSelector) is the fastest and most efficient way to do this.

根据本文档,find方法将在元素树中搜索,直到在选择器参数中找到元素。所以$(parentSelector).find(childSelector)是最快速、最有效的方法。

#2


16  

$('#thisElement').find('.classToSelect') will find any descendents of #thisElement with class classToSelect.

find('.classToSelect')将查找带有类classToSelect的#thisElement的任何后代。

#3


9  

This should do the trick:

这应该能达到目的:

$('#thisElement').find('.classToSelect')

#4


5  

Try this

试试这个

$('#thisElement .classToSelect').each(function(i){
         // do stuff
});

Hope it will help

希望它能帮助