• Jquery替换已存在于element上的event


         如何替换获去除那些已存在于某个element上的事件呢? 看下面code:

       1:  <head>
       2:  <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
       3:  <title>TestPage</title>
       4:  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript"></script>
       5:  <script type="text/javascript">
       6:   $(document).ready(function() {
       7:     $("#btn1").unbind('click').removeAttr('onclick').click(function() { 
       8:         alert('The method has invoked by Jquery.');
       9:     }); 
      10:   });
      11:  function foo()
      12:  {
      13:     alert('The method has invoked.');
      14:  }
      15:  </script>
      16:  </head>
      17:   
      18:  <body>
      19:  <input type="button" value="ClickMe" id="btn1" onclick="foo()" />
      20:  </body>
      21:   
      22:  </html>

    用的就是unbind,然后再removeAttr,最后再加上你的方法。来看下它们API document:

    unbind([type], [fn])

    概述

    bind()的反向操作,从每一个匹配的元素中删除绑定的事件。

    如果没有参数,则删除所有绑定的事件。

    你可以将你用bind()注册的自定义事件取消绑定。

    如果提供了事件类型作为参数,则只删除该类型的绑定事件。

    如果把在绑定时传递的处理函数作为第二个参数,则只有这个特定的事件处理函数会被删除。

    参数
    type (可选)String

    事件类型

    fn (可选)Function

    要从每个匹配元素的事件中反绑定的事件处理函数

    removeAttr(name)

    概述

    从每一个匹配的元素中删除一个属性

    参数
    nameString

    要删除的属性名

    简单的就是:

       1:     $("#someelement").unbind('eventname').removeAttr('event').click(function() { 
       2:        //your new method.
       3:     }); 

    希望对您有帮助。

    Author:Petter Liu   http://wintersun.cnblogs.com

  • 相关阅读:
    (转)投票系统,更改ip刷票
    图像判断(转)
    第06组 Alpha事后诸葛亮
    第06组 Alpha冲刺(6/6)
    2019 SDN上机第4次作业
    第06组 Alpha冲刺(5/6)
    第06组 Alpha冲刺(4/6)
    第06组 Alpha冲刺(3/6)
    第06组 Alpha冲刺(2/6)
    2019 SDN上机第3次作业
  • 原文地址:https://www.cnblogs.com/wintersun/p/1681760.html
Copyright © 2020-2023  润新知