jQuery event.stopPropagation() 方法
實例
阻止 click 事件冒泡到父元素:
$("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();
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()
參數 | 描述 |
---|---|
event | 必需。event 參數來自事件綁定函數。 |