The bind and Unbind functions of JQuery

Source: Internet
Author: User

Test:
Page code:
<body> <input type= "button" Name= "AAA" value= "click me" > <input type= "checkbox" Name= "CheckBox1" ></b Ody>
jquery Code:
$ (). Ready (function () {for (var i = 0; i < 3; i++) {$ ("input[type= ' button ']"). Click (function () {          Alert ("AAAA");      }); }
}
Alert ("AAAA") executes three times, in event nesting events, you do not want to see this situation, you need to disable the upper-level event, you can introduce the BIND and Unbind function to resolve.
To introduce a function:
for (var i = 0; i < 3; i++) {$ ("input[type= ' button ']"). Unbind ("click");        $ ("input[type= ' button ']"). Bind ("click", Function () {alert ("AAA"); }); }
Alert ("AAA"), executed only once. The bind () method adds one or more event handlers to the selected element and specifies the function to run when the event occurs
Unbind ()method to remove the event handler for the selected element. Ability to remove all or selected event handlers, or to terminate the operation of a specified function when an event occurs.
Events are event types, and the types are: Blur, Flcus, load, resize, scroll, unload, click, DBLCLIKC, MouseDown, MouseUp, MouseMove, MouseOver, Mouseout, MouseEnter, MouseLeave, change, select, Submit, KeyDown, KeyPress, KeyUp, and error, of course, can also be custom names.
Data is an optional parameter, and the composition Event.data the property value to the extra data object of the event object.
function is the handler function used to bind.
Syntax:
$ (selector). bind (Event,data,function)//event and function must indicate

Here are some code instructions:

Example 1: Delete all events of P

$ ("P"). Unbind ();

Example 2: Delete the Click event for P

$ ("P"). Unbind ("click");

Example 2: Delete the test function that starts after the P element click event and the test function that is triggered after the add P element Click event

$ ("P"). Unbind ("click", test); $ ("P"). Bind ("click", test);

Note: You must specify what events and functions to define. BIND ()

Now to see a simple demo, the entire div has a click-to-start event, if you want to click the link but does not trigger the DIV Click event, need to trigger the link when the div's Click event is disabled, here I use the link MouseEnter event is unbind Delete div event. Here is not finished, this time as long as the mouse into the link area, Div's Click event is deleted, we also need to join the mouse to move out of the link area when the div click event Recovery. The code is as follows:

12345678910
$(function () {varFunc= function () {    $(". Com2").Toggle (200); }        $(". Test").ClickFunc)        $(". Test a").MouseEnter (function () {                $(". Test").Unbind (); Delete all events for. Test});        $(". Test a").MouseLeave (function () {                $(". Test").Bind ("click",Func); Add click event}); }); event is the type
...
function is the handler function used to bind.
some of the content comes fromhttp://www.dearoom.com/blog/detailed unbind and bind/http://www.dearoom.com/blog/Event capture event bubbling and blocking event bubbling/

The bind and Unbind functions of JQuery

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.