兼容各大浏览器的JavaScript阻止事件冒泡代码
这里仅仅是一个简单代码demo,因为时间问题并未做深入研究,因为今天做项目时要用到阻止事件冒泡的内容,找了好多才找到一个可以使用的,特记录之。
<!DOCTYPEHTML>
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8">
<title>阻止事件冒泡</title>
<scriptsrc="js/jquery-1.11.3.min.js"></script>
<scriptsrc="js/jquery.cookie.js"></script>
<styletype="text/css">
</style>
<scripttype="text/javascript">
functionclickDiv(){
alert("clickDiv");
}
functionclickP(event){
stopEvent(event);
alert("clickP");
}
functionstopEvent(event){//阻止冒泡事件
//取消事件冒泡
vare=arguments.callee.caller.arguments[0]||event;//若省略此句,下面的e改为event,IE运行可以,但是其他浏览器就不兼容
if(e&&e.stopPropagation){
//thiscodeisforMozillaandOpera
e.stopPropagation();
}elseif(window.event){
//thiscodeisforIE
window.event.cancelBubble=true;
}
}
</script>
</head>
<body>
<divonclick="clickDiv()"style="width:100px;height:100px;background-color:red;">
<ponclick="clickP(event)"style="width:50px;height:50px;margin:auto;background-color:green;">
abad
</p>
</div>
<scripttype="text/javascript">
</script>
</body>
</html>
以上所述就是本文的全部内容了,希望大家能够喜欢。
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下