簡體   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>

如果單擊第三個div ,例如$(.three).click(function (){ }); 我需要父div.one )的類。 如何在jQuery中做到這一點?

嘗試使用: $('.three').parents('.one')
或使用$('.three').closest('.one')

從DOCS: http//api.jquery.com/parents/
從DOCS中獲取: http//api.jquery.com/closest/

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

一個通用的解決方案是:

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

所以:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM