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 参数来自事件绑定函数。 |