zoukankan      html  css  js  c++  java
  • 阻止JavaScript事件冒泡传递(cancelBubble 、stopPropagation)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="gb2312">
    <head>
    <title> 阻止JavaScript事件冒泡传递(cancelBubble 、stopPropagation)</title>
    <meta name="keywords" content="JavaScript,事件冒泡,cancelBubble,stopPropagation" />
    <script type="text/javascript">
    function doSomething (obj,evt) {
    alert(obj.id);
    var e=(evt)?evt:window.event;
    if (window.event) {
    e.cancelBubble=true;
    } else {
    //e.preventDefault();
    e.stopPropagation();
    }
    }
    </script>
    </head>
    <body>
    <div id="parent1" onclick="alert(this.id)" style="250px;background-color:yellow">
    <p>This is parent1 div.</p>
    <div id="child1" onclick="alert(this.id)" style="200px;background-color:orange">
    <p>This is child1.</p>
    </div>
    <p>This is parent1 div.</p>
    </div>
    <br />
    <div id="parent2" onclick="alert(this.id)" style="250px;background-color:cyan;">
    <p>This is parent2 div.</p>
    <div id="child2" onclick="doSomething(this,event);" style="200px;background-color:lightblue;">
    <p>This is child2. Will bubble.</p>
    </div>
    <p>This is parent2 div.</p>
    </div>
    </body>
    </html>
  • 相关阅读:
    jmeter配置mysql数据库步骤
    postman断言分析
    API测试工具postman使用总结
    量化投资与Python之NumPy
    量化投资与Python
    排序
    node.js
    VUE之搭建脚手架
    VUE之ECMAScript6(es6)
    VUE之随笔小总结1
  • 原文地址:https://www.cnblogs.com/xinzhuangzi/p/4100639.html
Copyright © 2011-2022 走看看