2012-03-20 209 views
6

如何将选择器从$(this)扩展到,可以说,它是儿童?或者其他任何东西。我的意思是,如何用$(this)正确地控制它们?

像:

$("li.nav").each(function() { 
    $(this AND children of this, or this AND it's siblings).something(); 
}); 

抱歉,如果这已经回答了,找不到它。

回答

8

您可以使用andSelf来实现:

$(this).children().andSelf().something(); 

您还可以使用.end弹出最后过滤操作切断电流链。所以,如果你想要孩子兄弟姐妹,你可以完成它:

$(this) 
    .children() // children of "this" 
    .end()   // now we're back to "this" 
    .siblings() // siblings of "this" 
    .andSelf()  // and "this" itself 
    .something(); 
+0

哈哈,这是最快为止。谢谢! – cincplug 2012-03-20 12:27:28

+0

@cincplug:没问题!乐意效劳。 – 2012-03-20 12:31:02