考虑以下几点:
<div onclick="alert('you clicked the header')" class="header">
<span onclick="alert('you clicked inside the header');">something inside the header</span>
</div>
我怎样才能做到当用户点击 span 时,它不会触发 div
的点击事件?
最佳答案
<span onclick="event.stopPropagation(); alert('you clicked inside the header');">something inside the header</span>
对于 IE:window.event.cancelBubble = true
<span onclick="window.event.cancelBubble = true; alert('you clicked inside the header');">something inside the header</span>
关于javascript - 如何使用内联 onclick 属性停止事件传播?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/387736/