这是由 hookee 回复于:2009-09-29 22:02:59 的答案。
可不可以将这个防止事件冒泡的方法提成一个公用方法?也就是说我们直接给它传递一个方法函数,这样就不用在每个页面都加入这段代码了。
------<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("divPromptBox").contains(obj)) return;
alert('');
}
</script>
可不可以将这个防止事件冒泡的方法提成一个公用方法?也就是说我们直接给它传递一个方法函数,这样就不用在每个页面都加入这段代码了。
------<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("divPromptBox").contains(obj)) return;
alert('');
}
</script>
function stopBubble(e){
//一般用在鼠标或键盘事件上
if(e && e.stopPropagation){
//W3C取消冒泡事件
e.stopPropagation();
}else{
//IE取消冒泡事件
window.event.cancelBubble = true;
}
};
你的方式你自己试试看,不要随随便便乱贴。不是说了不行吗!
<SCRIPT language=JavaScript type=text/javascript defer>
//@container 最外围的元素
//@action 事件处理函数名
//@evt 事件
function doit(container, action, evt){
evt = window.event?window.event:evt;
var obj = evt.toElement || evt.relatedTarget;
if(contains(container, obj)) return;
if(typeof action == "function") action(container, evt);
function contains(container, subobj){
if(document.all) return container.contains(subobj);
if(container==subobj) return true;
while(subobj=subobj.parentNode) if(container==subobj) return true;
return false;
}
}
</SCRIPT>
<div id="abc" style="background:#CCC" onmouseout="doit(this,function(){alert('');},event)">
<br><br><br>
<a href="xxx">xxxxx</a><br><br><br>
</div>