想要阻止點擊 #content 區域時觸發a事件,需要在 #content 區域內加入阻止事件冒泡的代碼,具體代碼如下:
<div id="box" onclick="a()"> <div id=content> </div> </div>
#box 包括 #content ,當點擊 #box 區域任何位置時(包括 #content ),都會觸發a事件。想要阻止點擊 #content 區域時觸發a事件,需要在 #content 區域內加入阻止事件冒泡的代碼。 代碼如下:
<div id="box" onclick="a()"> <div id="content" onclick="stopBubble(this.id)"> </div> </div>
function a(){ //這里是a事件的代碼 } function stopBubble(e) { if (e && e.stopPropagation) {//非IE瀏覽器 e.stopPropagation(); } else {//IE瀏覽器 window.event.cancelBubble = true; } }