且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

单击子元素时,如何阻止 onclick 事件触发父元素?

更新时间:2021-12-24 04:37:06

第一个问题:IE不支持stopPropagation(),你应该使用e.cancelBubble = true代码>.只需先做一个函数检查,看看你应该使用哪种方法(if (e.stopPropagation) {code}).

For your first question: IE doesn't support stopPropagation(), instead you should use e.cancelBubble = true. Just do a function check first to find out which method you should use (if (e.stopPropagation) {code}).

对于您的第二个问题:也许包括处理点击事件的第二个 div?

For your second question: Maybe include a second div that handles the click event?

<div><img src="image.jpg"/></div>
<div style="position:absolute" onclick="doSomething();"></div>

然后正确定位第二个div

and then position the second div correctly