【问题标题】:Extend jquery $(this)扩展 jquery $(this)
【发布时间】:2012-04-04 21:52:44
【问题描述】:
如何将选择器从 $(this) 扩展到,比如说,它是孩子?或其他任何事情。我的意思是,如何正确地将它们与 $(this) 连接起来?
喜欢:
$("li.nav").each(function() {
$(this AND children of this, or this AND it's siblings).something();
});
抱歉,如果这个问题已经回答了,但找不到。
【问题讨论】:
标签:
jquery
jquery-selectors
this
【解决方案1】:
您可以使用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();