<div class="one">
<div class="two">
<div class="three">success</div>
</div>
</div>
如果我點擊第三div
,例如$(.three).click(function(){ });
我需要的類的父div
(.one
)的。我怎麼能在jQuery中做到這一點?如何通過使用第三個子類獲取父級div值?
<div class="one">
<div class="two">
<div class="three">success</div>
</div>
</div>
如果我點擊第三div
,例如$(.three).click(function(){ });
我需要的類的父div
(.one
)的。我怎麼能在jQuery中做到這一點?如何通過使用第三個子類獲取父級div值?
您可以試用.parent().parent()
,例如, $(".three").parent().parent().anything(...)
。
嘗試:$('.three').parents('.one')
或$('.three').closest('.one')
從DOCS:http://api.jquery.com/parents/
從DOCS:http://api.jquery.com/closest/
一般的解決辦法是:
$(this).closest(".outer");
這樣:
$(".anyDepth").click(function() {
$(this).closest(".outermostDiv").addClass("foo");
});
THANKS BUDDIES我期待UR繼續提供服務 –
refff http://stackoverflow.com/questions/545978/finding-the-id-of-a-parent-div-using-jquery – Dgo