网站首页  汉语字词  英语词汇  考试资料  写作素材  旧版资料

请输入您要查询的范文:

 

标题 jQuery中on绑定事件后引发的事件冒泡问题如何解决
范文
    用on绑定时,我把子元素的 绑定到 document,而把父元素绑定到上级元素,导致 return false 阻止冒泡无效。
    代码如下:
    <!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">
    <head>
    <title>事件冒泡</title>
    <script src="jquery-1.7.1.js" type="text/javascript"></script>
    <script language="javascript" type="text/javascript">
    $(function () {
    $(document).on("click","#p1",function(e){
    console.log(e.target.tagName);
    console.log("p1被点击了");
    //e.stopPropagation(); //终止冒泡的方法
    return false;
    })
    $("#aa").on("click","#td1",function(e){
    console.log(e.target.tagName);
    console.log("td1被点击了");
    })
    $("#aa").on("click","#tr1",function(e){
    console.log(e.target.tagName);
    console.log("tr1被点击了");
    })
    $("#aa").on("click","#table1",function(e){
    console.log(e.target.tagName);
    console.log("table1被点击了");
    })
    });
    </script>
    </head>
    <body id="aa">
    <table onclick="alert('这是table')">
    <tr onclick="alert('这是tr')">
    <td onclick="alert('这是td')">
    <p onclick="alert('这是p')">段落</p>
    </td>
    </tr>
    </table>
    <table id="table1">
    <tr id="tr1">
    <td id="td1">
    <p id="p1">你好</p>
    </td>
    </tr>
    </table>
    </body>
    </html>
    on方法 将click等事件绑定在document对象上,页面上任何元素发生的click事件都冒泡到document对象上得到处理。
    增加了绑定效率。当事件冒泡到document对象时,检测事件的target,如果与传入的选择符(这里是button)匹配,就触发事件,否则不触发。
    修改为统一绑定对象后即解决,初步认为是因为 on方法的绑定机制问题。
    所以return false 无效。子元素和父元素修改为相同 绑定元素后,问题解决
    代码如下:
    <!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">
    <head>
    <title>事件冒泡</title>
    <script src="jquery-1.7.1.js" type="text/javascript"></script>
    <script language="javascript" type="text/javascript">
    $(function () {
    $("#aa").on("click","#p1",function(e){
    console.log(e.target.tagName);
    console.log("p1被点击了");
    //e.stopPropagation(); //终止冒泡的方法
    return false;
    })
    $("#aa").on("click","#td1",function(e){
    console.log(e.target.tagName);
    console.log("td1被点击了");
    })
    $("#aa").on("click","#tr1",function(e){
    console.log(e.target.tagName);
    console.log("tr1被点击了");
    })
    $("#aa").on("click","#table1",function(e){
    console.log(e.target.tagName);
    console.log("table1被点击了");
    })
    });
    </script>
    </head>
    <body id="aa">
    <table onclick="alert('这是table')">
    <tr onclick="alert('这是tr')">
    <td onclick="alert('这是td')">
    <p onclick="alert('这是p')">段落</p>
    </td>
    </tr>
    </table>
    <table id="table1">
    <tr id="tr1">
    <td id="td1">
    <p id="p1">你好</p>
    </td>
    </tr>
    </table>
    </body>
    </html>
    以上所述是小编给大家介绍的jQuery中on绑定事件后引发的事件冒泡问题及解决办法,希望能够帮助到大家!
随便看

 

在线学习网范文大全提供好词好句、学习总结、工作总结、演讲稿等写作素材及范文模板,是学习及工作的有利工具。

 

Copyright © 2002-2024 cuapp.net All Rights Reserved
更新时间:2025/5/15 17:49:52