When clicking on a button, execute the first event handler, and stop the rest of the event handlers from being executed:
var x = document.getElementById("myBtn");
x.addEventListener("click", myFunction);
x.addEventListener("click", someOtherFunction);
function myFunction(event) {
alert ("Hello World!");
event.stopImmediatePropagation();
}
// This function will not be executed
function someOtherFunction() {
alert ("I will not get to say Hello World");
}
Try it Yourself »
The stopImmediatePropagation() method prevents other listeners of the same event from being called.
The numbers in the table specify the first browser version that fully supports the method.
Method | |||||
---|---|---|---|---|---|
stopImmediatePropagation() | Yes | 9.0 | Yes | Yes | Yes |
event.stopImmediatePropagation()
None |
Return Value: | No return value |
---|---|
DOM Version: | DOM Level 3 Events |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!