简体   繁体   English

如何通过使用第三个子类获取父div值?

[英]how to get the parent div value by using third child class?

<div class="one">
    <div class="two">
        <div class="three">success</div>
    </div>
</div>

If I click the third div , for example $(.three).click(function (){ }); 如果单击第三个div ,例如$(.three).click(function (){ }); I need the class of the parent div ( .one ). 我需要父div.one )的类。 How can I do this in jQuery? 如何在jQuery中做到这一点?

try with: $('.three').parents('.one') 尝试使用: $('.three').parents('.one')
or with $('.three').closest('.one') 或使用$('.three').closest('.one')

from the DOCS: http://api.jquery.com/parents/ 从DOCS: http//api.jquery.com/parents/
from the DOCS: http://api.jquery.com/closest/ 从DOCS中获取: http//api.jquery.com/closest/

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

A general solution would be: 一个通用的解决方案是:

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

so: 所以:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM