【问题标题】:How to select clicked div and its child anchor element如何选择单击的 div 及其子锚元素
【发布时间】:2012-07-28 08:16:24
【问题描述】:

所以我有这种情况

<div class='linkDiv'>
<a href='#' class='link'>
Click here
</a>
</div>

当您单击 linkDiv 时,我想将相同的类添加到 .linkDiv 和 .link 但只有单击的类

$('.linkDiv').click(function(){
//some other code that works fine
//atm i have this
$(this).addClass('active');
$('.link', this).addClass('active');
});

如何同时选择被点击的 div 和该 div 的子锚点?

我试过了:

$('.linkDiv, .link', this)

但它只选择了.link

【问题讨论】:

    标签: jquery


    【解决方案1】:

    您可以将find()andSelf() 一起使用:

    $(".linkDiv").click(function() {
        $(this).find(".link").andSelf().addClass("active");
    });
    

    【讨论】:

      【解决方案2】:
      $(".linkDiv").click(function() {
       $(this).addClass("active");
       $(this).children().addClass("active");
      });
      

      【讨论】:

        猜你喜欢
        • 2012-05-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-27
        • 1970-01-01
        相关资源
        最近更新 更多