【问题标题】:how to get the parent div value by using third child class?如何使用第三个子类获取父 div 值?
【发布时间】:2012-05-14 18:47:41
【问题描述】:
<div class="one">
    <div class="two">
        <div class="three">success</div>
    </div>
</div>

如果我点击第三个div,例如$(.three).click(function (){ }); 我需要父类div (.one)。如何在 jQuery 中做到这一点?

【问题讨论】:

标签: javascript jquery css-selectors parents


【解决方案1】:

尝试:$('.three').parents('.one')
$('.three').closest('.one')

来自文档:http://api.jquery.com/parents/
来自文档:http://api.jquery.com/closest/

【讨论】:

    【解决方案2】:

    您可以尝试使用.parent().parent(),例如$(".three").parent().parent().anything(...).

    【讨论】:

      【解决方案3】:

      一般的解决方案是:

      $(this).closest(".outer");
      

      所以:

      $(".anyDepth").click(function () { 
          $(this).closest(".outermostDiv").addClass("foo");
      });
      

      【讨论】:

      • 感谢好友,我期待您继续提供服务
      猜你喜欢
      • 2014-09-02
      • 1970-01-01
      • 2011-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多