js阻止事件冒泡
<div id="test" onmouseout="doit(event)">
<img src="1.jpg" alt="" width="201" height="120">
<p>hello</p>
</div>
<img src="1.jpg" alt="" width="201" height="120">
<p>hello</p>
</div>
<script>
if(typeof(HTMLElement)!="undefined")
HTMLElement.prototype.contains=function(obj)
{
if(obj==this)return true;
while(obj=obj.parentNode) if(obj==this) return true;
return false;
}
function doit(evt){
evt = window.event?window.event:evt;
var obj = evt.toElement || evt.relatedTarget
if(document.getElementById("test").contains(obj)) return;//阻止事件冒泡的关键代码
alert('dsfsfda');
}
</script>
if(typeof(HTMLElement)!="undefined")
HTMLElement.prototype.contains=function(obj)
{
if(obj==this)return true;
while(obj=obj.parentNode) if(obj==this) return true;
return false;
}
function doit(evt){
evt = window.event?window.event:evt;
var obj = evt.toElement || evt.relatedTarget
if(document.getElementById("test").contains(obj)) return;//阻止事件冒泡的关键代码
alert('dsfsfda');
}
</script>
上段代码意思是当鼠标移出Div区域就执行doit函数,为了防止当鼠标移至p标签上时就执行该函数,加上以上JS代码就可以搞定。

浙公网安备 33010602011771号