HTML:
<div id="aaa" class="p">
<div id = "bbb">
<div id="actor" onclick = "testParent(this)">
test parent
</div>
</div>
</div>
JS:
testParent = function(obj){
alert($(obj).parent(".p").id);
}
我认为这应该令人警觉,aaa
但事实并非如此。
这不是按班级找家长的方法吗?
编辑我知道最近的人可以做到这一点,但更具体地说是父母,不是吗?因此,jQuery对此没有特定的实现?
尝试:
testParent = function(obj){
alert($(obj).parents('.p').attr('id'));
}
这是非常简单而简短的代码。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句