设置或者取消事件冒泡<SCRIPT LANGUAGE="JScript">
function checkCancel()
{
if (window.event.shiftKey)
window.event.cancelBubble = true;
}
function showSrc()
{
if (window.event.srcElement.tagName == "IMG")
alert(window.event.srcElement.src);
}
</SCRIPT><BODY onclick="showSrc()">
<IMG onclick="checkCancel()" SRC="sample.gif">
function checkCancel()
{
if (window.event.shiftKey)
window.event.cancelBubble = true;
}
function showSrc()
{
if (window.event.srcElement.tagName == "IMG")
alert(window.event.srcElement.src);
}
</SCRIPT><BODY onclick="showSrc()">
<IMG onclick="checkCancel()" SRC="sample.gif">
使因本次动作引发的后续事件不发生
但如果你在点击图片的同时按下了shift键,则checkCancel()函数使event的cancelBubble = true,它阻止了下一个body体的click事件的发生。我的理解对吗?
大虾阿,有没有好一点的办法??
<div onclick="alert('最外层')">
<span onclick="window.event.cancelBubble=true;alert('outer span')">
<span onclick="alert(this.tagName);">this is a test!</span>
<button onclick="window.event.cancelBubble=true" id=btn1 name=button1>cancel1</button></span>
</div>
<button onclick="window.event.cancelBubble=true" =btn2>cancel2</button>为什么点击btn1 后,冒泡事件并没取消?
点击 btn2 也不管用?
<span onclick="window.event.cancelBubble=true;alert('outer span')">
<span onclick="alert(this.tagName);">this is a test!</span>
</span>
</div>
<div onclick="alert('最外层2')">
<span onclick="alert('outer span')">
<span onclick="alert(this.tagName);">this is a test!</span>
</span>
</div>上面两个都会引发气泡事件,想都取消,一个语句怎么写??