How to replace and remove the events that already exist on an element? See the following code:
1: 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: 17:
18: <body>
19: <input type = "button" value = "ClickMe" id = "btn1" onclick = "foo ()"/>
20: </body>
21:
22:
The unbind is used, removeAttr is used, and your method is added. Let's take a look at their API document:
Unbind ([type], [fn])
Overview
Bind () reverse operation to delete binding events from each matching element.
If no parameter exists, all bound events are deleted.
You can unbind the custom event you registered with bind.
If the event type is provided as a parameter, only binding events of this type are deleted.
If the handler passed during binding is used as the second parameter, only this specific event handler will be deleted.
Parameters
Type (optional) String
Event Type
Fn (optional) Function
Event Handler to be unbound from each event that matches the element
RemoveAttr (name)
Overview
Deletes an attribute from each matching element.
Parameters
NameString
Attribute name to be deleted
Simple:
1: $ ("# someelement"). unbind ('eventname'). removeAttr ('event'). click (function (){
2: // your new method.
3 :});
Hope to help you.
Author: Petter Liu http://wintersun.cnblogs.com