【问题标题】: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();
    

    【讨论】:

    • 哈哈,这是目前最快的。谢谢!
    • @cincplug:没问题!很高兴能提供帮助。
    猜你喜欢
    • 2013-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-13
    • 2013-05-23
    • 2012-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多