<div class="one">
<div class="two">
<div class="three">success</div>
</div>
</div>
divたとえば 、3 番目の をクリックする$(.three).click(function (){ });
と、親div( .one) のクラスが必要になります。jQueryでこれを行うにはどうすればよいですか?
<div class="one">
<div class="two">
<div class="three">success</div>
</div>
</div>
divたとえば 、3 番目の をクリックする$(.three).click(function (){ });
と、親div( .one) のクラスが必要になります。jQueryでこれを行うにはどうすればよいですか?
試してみてください:$('.three').parents('.one')
または$('.three').closest('.one')
ドキュメントから: http://api.jquery.com/parents/
ドキュメントから: http://api.jquery.com/closest/
.parent().parent()たとえば、で試すことができ$(".three").parent().parent().anything(...)ます。
一般的な解決策は次のとおりです。
$(this).closest(".outer");
それで:
$(".anyDepth").click(function () {
$(this).closest(".outermostDiv").addClass("foo");
});