jQuery:选择祖父母
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2369913/
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: selecting grandparents
提问by aneuryzm
Is there a better way to select grandparent elements in jQuery in order to avoid this ?
是否有更好的方法在 jQuery 中选择祖父元素以避免这种情况?
$(this).parent().parent().parent().parent().parent().children(".title, .meta").fadeIn("fast");
Thanks.
谢谢。
回答by Neil Aitken
You can use the parents()
method which matches parents against a selector
您可以使用将parents()
父项与选择器匹配的方法
http://api.jquery.com/parents/
http://api.jquery.com/parents/
Or if you're using 1.4 there is a new parentsUntil()
method
或者,如果您使用的是 1.4,则有一种新parentsUntil()
方法
回答by aneuryzm
In addition to parents()
, as they've said, you should also check out closest()
. Read the comparison in the documentation there, but its main differences are that it searches for only one result, and it includes $(this)
in what it searches (could get the thing you're searching from if you're not specific enough). Pros and cons.
此外parents()
,正如他们所说,您还应该查看closest()
. 阅读那里的文档中的比较,但它的主要区别在于它只搜索一个结果,并且它包含$(this)
在它搜索的内容中(如果你不够具体,可能会得到你正在搜索的东西)。利弊。
回答by Femi
I wrote this simple plugin because I thought I had an unambiguous class selection somewhere giving me errors. Selecting grandparent seemed more direct than $().parents()
for this particular case.
我写了这个简单的插件,因为我认为我在某处有一个明确的类选择给我错误。选择祖父母似乎比$().parents()
这个特殊情况更直接。
Well, I used this once then realized I actually had a spelling error. Not sure how helpful it really is. $('myelem').gparent(2);
gets you the grandparent of 'myelem'.
好吧,我用过一次,然后意识到我实际上有一个拼写错误。不确定它到底有多大帮助。$('myelem').gparent(2);
让你成为'myelem'的祖父母。
(function( $ ){
$.fn.gparent = function( recursion ){
if( recursion == undefined ) recursion = 2;
if(typeof(recursion) == "number"){
recursion = parseInt( recursion );
if( recursion > 0 ){
gparent_holder = $(this);
for(var gparent_i = 0; gparent_i < recursion; gparent_i++){
gparent_holder = gparent_holder.parent();
}
return gparent_holder;
}
else return false;
}
else return false;
}
})( jQuery );
回答by patrick
@Femi had an answer to do this, and mentioned recursion, yet his solution was not recursive, here's a recursive jQuery solution for getting ancestors of an item:
@Femi 有一个答案可以做到这一点,并提到了递归,但他的解决方案不是递归的,这是一个用于获取项目祖先的递归 jQuery 解决方案:
$.fn.gparent = function( recursion ){
console.log( 'recursion: ' + recursion );
if( recursion > 1 ) return $(this).parent().gparent( recursion - 1 );
return $(this).parent();
};
If this is your HTML:
如果这是您的 HTML:
<div id='grandparent'>
<div id='parent'>
<div id='child'>
child
</div>
</div>
</div>
You can call
你可以打电话
console.log( $('#child').gparent( 2 ) );
to get the grandparent of element child
. Here's the JSFiddle
获取 element 的祖父母child
。这是 JSFiddle
回答by Traveling Tech Guy
Use the parents()
selector to get all parents of an element. You can then either search the collection, or iterate over it if you want to affect all ancestors.
使用parents()
选择器获取元素的所有父元素。然后,您可以搜索集合,或者如果您想影响所有祖先,则对其进行迭代。
回答by NJENGAH
Using parents()
and children()
get the same results.
使用parents()
并children()
得到相同的结果。
Example instead of using this :
示例而不是使用此:
$(this).parent().parent().parent().children(".title").fadeIn("fast");
you can use this :
你可以使用这个:
$(this).parents().children(".title").fadeIn("fast");