简体   繁体   中英

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 (){ }); I need the class of the parent div ( .one ). How can I do this in jQuery?

try with: $('.three').parents('.one')
or with $('.three').closest('.one')

from the DOCS: http://api.jquery.com/parents/
from the 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");
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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