ฉันใช้มาร์กอัปต่อไปนี้และได้เข้ารหัสปัญหาเดียวกัน:
<ul class="nav">
<li><a href="abc.html">abc</a></li>
<li><a href="def.html">def</a></li>
</ul>
ที่นี่ฉันได้ใช้ตรรกะต่อไปนี้:
$(".nav > li").click(function(e){
if(e.target != this) return; // only continue if the target itself has been clicked
// this section only processes if the .nav > li itself is clicked.
alert("you clicked .nav > li, but not it's children");
});
ในแง่ของคำถามที่แน่นอนฉันจะเห็นว่าการทำงานดังต่อไปนี้:
$(".example").click(function(e){
if(e.target != this) return; // only continue if the target itself has been clicked
$(".example").fadeOut("fast");
});
หรือแน่นอนวิธีอื่น ๆ :
$(".example").click(function(e){
if(e.target == this){ // only if the target itself has been clicked
$(".example").fadeOut("fast");
}
});
หวังว่าจะช่วย