扩展jquery $(this)

cin*_*lug 6 jquery this jquery-selectors

如何将选择器从$(this)扩展到,让我们说,它是孩子们?或者其他任何事情.我的意思是,如何用$(this)正确地将它们连接起来?

喜欢:

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

对不起,如果已经回答,找不到它.

And*_*ker 8

你可以andSelf用来完成这个:

$(this).children().andSelf().something();
Run Code Online (Sandbox Code Playgroud)

您还可以使用.end弹出当前链的最后一个过滤操作.所以如果你想要孩子兄弟姐妹,你也可以做到这一点:

$(this)
    .children()    // children of "this"
    .end()         // now we're back to "this"
    .siblings()    // siblings of "this"
    .andSelf()     // and "this" itself
    .something();
Run Code Online (Sandbox Code Playgroud)