我有一个针对整个文档的 onclick 事件处理程序:
document.onclick = function(objEvt){
alert("document click");
}
对于某些元素,我还需要一个特定的 onclick 事件处理程序:
$(divElementObj).click( function() {
alert("div click");
});
我总是只获取文档事件处理程序的输出。我必须做什么才能看到这两个警报?
最佳答案
您可以使用stopPropagation:
$(divElementObj).click( function(e) {
e.stopPropagation();
alert("div click");
});
<小时/>
What does the stopPropagation do? :
Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event.
关于javascript - 需要通用和特定的 onclick 事件处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24776980/