关于Js事件冒泡及阻止事件冒泡

来源:互联网 发布:网络钓鱼是什么意思 编辑:程序博客网 时间:2024/06/13 02:23

Js 代码

<script type="text/javascript">$(function(){    // 为span元素绑定click事件    // 为div元素绑定click事件    $('#content').bind("click",function(){        var txt = $('#msg').html() + "<p>外层div元素被点击.<p/>";        $('#msg').html(txt);    });    // 为body元素绑定click事件    $("body").bind("click",function(){        var txt = $('#msg').html() + "<p>body元素被点击.<p/>";        $('#msg').html(txt);    });    $('span').bind("click",function(event){        var txt = $('#msg').html() + "<p>内层span元素被点击.<p/>";//获取html信息        $('#msg').html(txt);// 设置html信息        //阻止事件冒泡        // return false;        // event.stopPropagation();    });})</script>

html 代码

<body><div id="content">    外层div元素    <span>内层span元素</span>    外层div元素</div><div id="msg"></div></body>


0 0