我试图找出按类选择元素的第n个子元素的语法,但是我不知道该元素的确切路径。我做不到$('parent > child > grandchild > hereIam');
所以基本上我需要能够说
$('#thisElement').AllRelativesWithClass('.classToSelect')
我该怎么做?
答案 0 :(得分:45)
根据这个documentation,find方法将向下搜索元素树,直到它在选择器参数中找到元素。所以$(parentSelector).find(childSelector)
是最快,最有效的方法。
答案 1 :(得分:23)
$('#thisElement').find('.classToSelect')
会找到#thisElement
的任何后代与classToSelect
课。
答案 2 :(得分:14)
这应该可以解决问题:
$('#thisElement').find('.classToSelect')
答案 3 :(得分:6)
试试这个
$('#thisElement .classToSelect').each(function(i){
// do stuff
});
希望它会有所帮助