jquery binding Event Monitor bind and remove event listener unbind usage examples _jquery

Source: Internet
Author: User
Tags event listener

This example describes the jquery binding event listening for bind and removal events listening unbind usage. Share to everyone for your reference, specific as follows:

In this case, the following bind (Eventtype,[data],listener)//data is used as an optional parameter, one () the event that is bound by the method is automatically deleted once after it is triggered, unbind (Eventtype,listener),

Instance:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
">  

Look at this test code again:

<body>
<input type= "button" Name= "AAA" value= "click me" >
<input type= "checkbox" Name= "CheckBox1" >
</body>

jquery Code:

$ (). Ready (function () {for
(var i = 0; i < 3; i++) {
$ ("input[type= ' button ']"). Click (function () {
alert (" AAAA ");}
);
}


Alert ("AAAA") executes three times, and in event nesting events, you do not want to see such a situation, you need to disable the upper level event, you can introduce 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");

The bind () method adds one or more event handlers for the selected element and sets the function to run when the event occurs
The Unbind () method removes an event handler for the selected element. The ability to remove all or selected event handlers, or terminate the operation of the specified function when an event occurs.

event types include: 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, the composition Event.data the attribute value to the event object's additional data object.
A function is a handler function that is used to bind.

Grammar:

$ (selector). bind (Event,data,function)

The event and function must indicate the following sections of code:

Example 1: Delete all events for P

Copy Code code as follows:
$ ("P"). Unbind ();

Example 2: Delete the Click event for P
Copy Code code as follows:
$ ("P"). Unbind ("click");

Example 2: Delete the test function after the P element click event and the test function triggered after adding the P element Click event

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

Note: To define. BIND () What events and functions must be specified

Now to see a simple demo, the entire div has a click to open up the event, if you want to click on the link but do not trigger the DIV Click event, need to trigger the link when the div Click event Disabled, here I use the link MouseEnter event is unbind Delete div event. Here is not finished, at this time as long as the mouse into the link area, the div Click event 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:

$ (function () {var Func = function () {
  $ (". Com2"). Toggle
  $ (". Test"). Click (Func)
  $ (". Test a"). MouseEnter (function () {
    $ (. test). Unbind ();//delete all events for. Test
  });
  $ (". Test a"). MouseLeave (function () {
    $ (". Test"). Bind ("click", Func);//Add click event
  });


Event is of type
...
A function is a handler function that is used to bind.

For more information on jquery event-related content readers can view the site: "A summary of common events in jquery usage and skills"

I hope this article will help you with the jquery program design.

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.