考虑以下:
<div onclick="alert('you clicked the header')" class="header"> <span onclick="alert('you clicked inside the header');">something inside the header</span> </div>
我怎样才能做到这一点,当用户点击跨度时,它不会触发div的点击事件?
div
使用event.stopPropagation()。
<span onclick="event.stopPropagation(); alert('you clicked inside the header');">something inside the header</span>
对于 IE:window.event.cancelBubble = true
window.event.cancelBubble = true
<span onclick="window.event.cancelBubble = true; alert('you clicked inside the header');">something inside the header</span>