Jquery replaces the event method that already exists on the element

Source: Internet
Author: User

See the following code:
Copy codeThe Code is as follows:
<Head>
<Meta content = "text/html; charset = UTF-8" http-equiv = "Content-Type"/>
<Title> TestPage </title>
<Script src = "http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type = "text/javascript"> </script>
<Script type = "text/javascript">
$ (Document). ready (function (){
$ ("# Btn1"). unbind ('click'). removeAttr ('onclick'). click (function (){
Alert ('the method has invoked by Jquery .');
});
});
Function foo ()
{
Alert ('the method has invoked .');
}
</Script>
</Head>
<Body>
<Input type = "button" value = "ClickMe" id = "btn1" onclick = "foo ()"/>
</Body>
</Html>

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:
Copy codeThe Code is as follows:
$ ("# Someelement"). unbind ('eventname'). removeAttr ('event'). click (function (){
// Your new method.
});

Hope to help you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.