事件冒泡

common.js

function  my$(id) {
    return  document.getElementById(id);
}

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        #div1{
            width: 300px;
            height: 200px;
            background-color: red;
        }
        #div2{
            width: 250px;
            height: 150px;
            background-color: green;
        }
        #div3{
            width: 200px;
            height: 100px;
            background-color: blue;
        }


    </style>
</head>
<body>
<div id="div1">
    <div id="div2">
        <div id="div3">


        </div>
    </div>
</div>
<script src="common.js"></script>
<script>
    //事件冒泡:多个元素嵌套,有层次关系,这些标签都注册了相同的事件,
   // 如果里面元素的事件触发了,外面的元素的该事件自动的触发了


    //事件冒泡,阻止事件冒泡
    //如何阻止事件冒泡 window.event.cancelBubble=true;火狐不支持
    //e.stopPropagation();ie8不支持
    my$("div1").οnclick=function () {
      console.log(this.id);
    };
    my$("div2").οnclick=function () {
        console.log(this.id);
        window.event.cancelBubble=true;
    };
    //事件处理参数对象 (参数不一定是e) 参数用着的时候写 用不着就不写
    // my$("div3").οnclick=function (e) {
    //     console.log(this.id);
    //  //   console.log(arguments.length);  1
    //     console.log(e);
    // };
    my$("div3").οnclick=function (e) {
        console.log(this.id);
    //阻止事件冒泡
        e.stopPropagation();
        console.log(e);//ie8不支持e事件处理参数对象
    };
    // document.body.οnclick=function () {
    //   console.log("我是body");
    // };//body的高度被子盒子撑开,所有body的高大约300px
</script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值