zoukankan      html  css  js  c++  java
  • JavaScript阻止事件冒泡(兼容IE、Chrome、FF)

    这里仅仅是一个简单代码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="100px; height:100px; background-color:red;">
    		<p onclick="clickP(event)" style="50px; height:50px; margin:auto; background-color:green;">
    			abad
    		</p>
    	</div>
    	<script type="text/javascript">
    	</script>
    </body>
    </html>
    

      

  • 相关阅读:
    Python作业之分页显示内容
    Codeforces Round #368 (Div. 2)
    数论专项测试——约数个数和(lucas的数论)
    数论专题测试——逆元
    数论专题测试——幸运数字
    bzoj2219: 数论之神
    bzoj3283: 运算器
    梅森素数
    后缀数组
    Hash_1014: [JSOI2008]火星人prefix
  • 原文地址:https://www.cnblogs.com/craftsman-gao/p/4597792.html
Copyright © 2011-2022 走看看