jQuery event.stopPropagation()วิธี การ
ตัวอย่าง
หยุดเหตุการณ์คลิกไม่ให้เดือดปุด ๆ ไปยังองค์ประกอบหลัก:
$("span").click(function(event){
event.stopPropagation();
alert("The span element was clicked.");
});
$("p").click(function(event){
alert("The p element was clicked.");
});
$("div").click(function(){
alert("The div element was clicked.");
});
ความหมายและการใช้งาน
เมธอด event.stopPropagation() จะหยุดการเดือดของเหตุการณ์ไปยังองค์ประกอบหลัก ป้องกันไม่ให้ตัวจัดการเหตุการณ์หลักถูกดำเนินการ
เคล็ดลับ:ใช้เมธอด event.isPropagationStopped()เพื่อตรวจสอบว่าเมธอดนี้ถูกเรียกสำหรับเหตุการณ์หรือไม่
ไวยากรณ์
event.stopPropagation()
Parameter | Description |
---|---|
event | Required. The event parameter comes from the event binding function |