ฉันมีรหัสต่อไปนี้
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
</head>
<div id="hello">Hello <div>Child-Of-Hello</div></div>
<br />
<div id="goodbye">Goodbye <div>Child-Of-Goodbye</div></div>
<script type="text/javascript">
<!--
function fun(evt) {
var target = $(evt.target);
if ($('div#hello').parents(target).length) {
alert('Your clicked element is having div#hello as parent');
}
}
$(document).bind('click', fun);
-->
</script>
</html>
ฉันคาดหวังเมื่อChild-Of-Hello
ถูกคลิกเท่านั้น$('div#hello').parents(target).length
จะกลับมา> 0
อย่างไรก็ตามมันจะเกิดขึ้นทุกครั้งที่ฉันคลิกที่ใดก็ได้
มีบางอย่างผิดปกติกับรหัสของฉันหรือไม่?