考虑以下:
<div onclick="alert('you clicked the header')" class="header"> <span onclick="alert('you clicked inside the header');">something inside the header</span> </div>
如何做到这一点,以便当用户单击范围时不会触发div的click事件?
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>