這里僅僅是一個簡單代碼demo,因為時間問題並未做深入研究,因為今天做項目時要用到阻止事件冒泡的內容,找了好多才找到一個可以使用的,特記錄之。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>阻止事件冒泡</title>
<script src="js/jquery-1.11.3.min.js"></script>
<script src="js/jquery.cookie.js"></script>
<style type="text/css">
</style>
<script type="text/javascript">
function clickDiv(){
alert("clickDiv");
}
function clickP(event){
stopEvent(event);
alert("clickP");
}
function stopEvent(event){ //阻止冒泡事件
//取消事件冒泡
var e=arguments.callee.caller.arguments[0]||event; //若省略此句,下面的e改為event,IE運行可以,但是其他瀏覽器就不兼容
if (e && e.stopPropagation) {
// this code is for Mozilla and Opera
e.stopPropagation();
} else if (window.event) {
// this code is for IE
window.event.cancelBubble = true;
}
}
</script>
</head>
<body>
<div onclick="clickDiv()" style="width:100px; height:100px; background-color:red;">
<p onclick="clickP(event)" style="width:50px; height:50px; margin:auto; background-color:green;">
abad
</p>
</div>
<script type="text/javascript">
</script>
</body>
</html>
