简体   繁体   English

DOM 0级事件:如何防止外部点击触发?

[英]DOM Level 0 event: how to prevent the outer click from firing?

Check the following code: 检查以下代码:

​<div onclick="alert('Hi, from outer div!');">
    <button onclick="alert('Hi, from button!');">Tha button</button>, Click me!
</div>​​​​

Is there a way to prevent the outer div from firing an onclick when I click the button? 有没有一种方法可以防止外部div在单击按钮时触发onclick? Any idea how to cancel DOM level 0 events?​​ 知道如何取消DOM 0级事件吗?

Note: I can't use jQuery. 注意:我不能使用jQuery。 It needs to work on Chrome, FireFox, IE6-9.​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ 它需要在Chrome,FireFox,IE6-9上运行。

Yes. 是。 In most standard browsers, you call stopPropagation on the event object ( live example | source ): 在大多数标准浏览器中,您对事件对象( 实时示例 | source )调用stopPropagation

​<div onclick="alert('Hi, from outer div!');">
    <button onclick="alert('Hi, from button!'); event.stopPropagation();">Tha button</button>, Click me!
</div>​​​​

In older copies of IE, you have to set the cancelBubble property to true instead: 在旧版的IE中,您必须将cancelBubble属性设置为true

​<div onclick="alert('Hi, from outer div!');">
    <button onclick="alert('Hi, from button!'); event.cancelBubble = false;">Tha button</button>, Click me!
</div>​​​​

...which means for broad compatibility you have to test which you're dealing with, which gets ugly ( live example | source ): ...这意味着要获得广泛的兼容性,您必须测试要处理的内容,这很难看( 实际示例 | 源代码 ):

​<div onclick="alert('Hi, from outer div!');">
    <button onclick="alert('Hi, from button!'); if (event.stopPropagation) { event.stopPropagation(); } else { event.cancelBubble = true; }">Tha button</button>, Click me!
</div>​​​​

These sorts of differences are why I always recommend moving away from the old DOM0-style handler and using a decent JavaScript library like jQuery , Prototype , YUI , Closure , or any of several others . 这些类型的差异是为什么我总是建议远离旧的DOM0样式处理程序,并使用像jQueryPrototypeYUIClosure其他几种这样的体面的JavaScript库的原因。 These smooth over differences between browsers and provide a huge amount of utility functionality. 这些可以消除浏览器之间的差异,并提供大量的实用程序功能。

For example, with jQuery, this HTML: 例如,对于jQuery,此HTML:

​<div id="theDiv">
    <button id="theButton">Tha button</button>, Click me!
</div>​​​​

...and this script ( live example | source ): ...以及此脚本( 实时示例 | 源代码 ):

$("#theDiv").click(function() {
    alert('Hi, from outer div!');
});
$("#theButton").click(function(event) {
    alert('Hi, from button!');
    event.stopPropagation(); // Even on IE, jQuery provides this
});

Or frequently with jQuery, you see people just doing return false; 或经常使用jQuery,您会发现人们只是在做return false; in their event handler. 在他们的事件处理程序中。 return false; in a handler, in jQuery , does two things: Stops propagation, and prevents any default action the event might have had (for instance, in a click handler on a link). 在处理程序中, 在jQuery中 ,有两件事:停止传播,并阻止事件可能采取的任何默认操作(例如,在链接的单击处理程序中)。 stopPropgation doesn't prevent the default. stopPropgation不会阻止默认设置。

But this isn't meant to be an advertisement for jQuery (though it is a very good library overall). 但这并不意味着要成为jQuery的广告(尽管它总体上是一个很好的库)。 Closure, YUI, Prototype, and all the others have similar functionality for letting you not worry about these sorts of browser incompatibilities. Closure,YUI,Prototype和其他所有功能都具有相似的功能,可让您不必担心这些浏览器不兼容的情况。

<div onclick="alert('Hi, from outer div!');">
    <button onclick="event.stopPropagation(); alert('Hi, from button!');">Tha button</button>, Click me!
</div>​

What I added was an event.stopPropagation(); 我添加的是一个event.stopPropagation(); What this does is that is stops the bubbling from occurring 这是在阻止起泡的发生

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM