zoukankan      html  css  js  c++  java
  • 阻止JavaScript事件冒泡传递<转>

    cancelBubble在IE下有效
    stopPropagation在Firefox下有效

    详细出处参考:http://www.jb51.net/article/9858.htm

    <!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>
    版权说明

      如果标题未标有<转载、转>等字则属于作者原创,欢迎转载,其版权归作者和博客园共有。
      作      者:温景良
      文章出处:http://wenjl520.cnblogs.com/  或  http://www.cnblogs.com/

  • 相关阅读:
    算法题-数组算法题
    Linux-shell脚本的调试和追踪
    Linux-循环loop
    Linux-条件判断式
    Linux-第一行#!/bin/bash的含义
    Linux-排序命令:sort、wc、uniq
    Linux-选取命令:cut grep
    Linux-shell变量
    Linux-重定向、追加、tee
    Linux三剑客-sed编辑文本
  • 原文地址:https://www.cnblogs.com/wenjl520/p/2036302.html
Copyright © 2011-2022 走看看