[英]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.