zoukankan      html  css  js  c++  java
  • 【JQ】jq动态绑定事件.on()、解绑事件off()

    #JQ 绑定与解绑事件的方法的历史演变

      1、 jquery1.4 及之前的版本,由.click() 或 .bind()方法绑定的事件,不能适用脚本创建的新元素即是说页面加载完成后,再动态创建的DOM元素并不能响应之前绑定的事件!解绑事件使用.unbind()方法;

      旧版本的处理方法是使用.live()方法来代替事件绑定.bind(),使得绑定的事件能适用脚本创建的新元素,从而实现事件的动态绑定;解绑事件使用.unlive()方法;

     1 <html>
     2 <head>
     3 <script type="text/javascript" src="/jquery/jquery.js"></script>
     4 <script type="text/javascript">
     5 $(document).ready(function(){
     6   $("p").live("click",function(){
     7     $(this).slideToggle();
     8   });
     9   $("button").click(function(){
    10     $("<p>This is a new paragraph.</p>").insertAfter("button");
    11   });
    12 });
    13 </script>
    14 </head>
    15 <body>
    16 <p>这是一个段落。</p>
    17 <p>点击任意 p 元素会令其消失。包括本段落。</p>
    18 <button>在本按钮后面插入新的 p 元素</button>
    19 <p><b>注释:</b>通过使用 live() 方法而不是 bind() 方法,新的 p 元素同样会在点击时消失。</p>
    20 </body>
    21 </html>

      在线测试.live()方法地址:http://www.w3school.com.cn/tiy/t.asp?f=jquery_event_live_newelement

      2、但自jq1.7版本开始,官方已明文表示不再推荐使用使用.live()方法,官方解释为:live()调用过程首先将事件方法绑定到了Document,然后,查找Document里是否有匹配元素。 这个过程对于性能来说可能比较浪费。官方推荐将.live()改成Delegate()方法,适用脚本动态创建的新元素。解绑使用.undelegate()方法;

     1 <html>
     2 <head>
     3 <script type="text/javascript" src="/jquery/jquery.js"></script>
     4 <script type="text/javascript">
     5 $(document).ready(function(){
     6   $("div").delegate("p","click",function(){
     7     $(this).slideToggle();
     8   });
     9   $("button").click(function(){
    10     $("<p>这是一个新段落。</p>").insertAfter("button");
    11   });
    12 });
    13 </script>
    14 </head>
    15 <body>
    16 <div style="background-color:yellow">
    17 <p>这是一个段落。</p>
    18 <p>请点击任意一个 p 元素,它会消失。包括本段落。</p>
    19 <button>在本按钮后面插入一个新的 p 元素</button>
    20 </div>
    21 <p><b>注释:</b>通过使用 delegate() 方法,而不是 live(),只有 div 元素中的 p 元素会受到影响。</p>
    22 </body>
    23 </html>

      在线测试.delegate()方法地址:http://www.w3school.com.cn/tiy/t.asp?f=jquery_event_delegate_newelement

      3、jq1.8开始,官方又再次申明:如果你开发最新版本的jQuery,完全可以使用on()方法来处理所有的事件绑定,避免过多的方法调用,因为其实在最新版本的jQuery类库中,所有以上旧版方法在后面其实都是调用on()方法。解绑事件使用.off()方法;

    #.on()与.off()的具体使用方法

      1、.on()的使用

    <!DOCTYPE html>
    <html>
    <head>
    <script src="https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js">
    </script>
    <script>
    $(document).ready(function(){
      $("#div1").on("click","p",function(){
        $(this).css("background-color","pink");
      });
    });
    </script>
    </head>
    <body>
    
    <h4 style="color:green;">This example demonstrates how to achieve the same effect using on() and delegate().</h4>
    
    <div id="div1">
    <p>Click to set background color using the <b>on() method</b>.</p>
    </div>
    
    </body>
    </html>

       添加多个事件处理程序方法:

    1 $(document).ready(function(){
    2   $("p").on("mouseover mouseout",function(){
    3     $("p").toggleClass("intro");
    4   });
    5 });

      2、.off()的使用

     1 <!DOCTYPE html>
     2 <html>
     3 <head>
     4 <meta charset="utf-8"> 
     5 <title>菜鸟教程(runoob.com)</title> 
     6 <script src="https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js">
     7 </script>
     8 <script>
     9 $(document).ready(function(){
    10   $("body").on("click","p",function(){
    11     $(this).css("background-color","pink");
    12   });
    13   $("#btn1").click(function(){
    14     $("body").off("click","p");
    15   });
    16 
    17 });
    18 </script>
    19 </head>
    20 <body>
    21 
    22 <h4 style="color:green;">该事件演示了如何使用 off() 和 undelegate() 方法来达到相同的效果。</h4>
    23 
    24 <p>点击段落修改背景颜色。</p>
    25 <p>点击以下按钮再点击这个段落 ( click 事件被移除)。</p>
    26 
    27 <button id="btn1">使用  off() 方法移除 click 事件</button>
    28 
    29 </body>
    30 </html>
  • 相关阅读:
    Annual Summary-2019
    (翻译向)kafka--简介篇1
    mysql 查看数据库大小
    nohup保证程序后台运行
    NLP(四):文本向量化word2vec
    NLP(三):关键词提取
    NLP(二):jieba高频词提取
    NLP(一):jieba分词
    爬虫(一)百度翻译
    【论文笔记】社交网络中的信息扩散分析及其应用研究
  • 原文地址:https://www.cnblogs.com/willingtolove/p/9594948.html
Copyright © 2011-2022 走看看