How to use the JQuery event delegate binding click

Source: Internet
Author: User

Direct Binding UL Click event

The code is as follows Copy Code

$ ("ul"). Click (Function (E)

Example

  code is as follows copy code

$ (function () {
 //$ ("ul"). On (' Click ', This,function (e) {
 $ ("UL"). Click (function (e) {
   $target = $ (e.target);
  if ($target. Is ("H3")) {
    alert ("H3");  
    }else if ($target. Is ("a.a1")
  {
    alert ("a.a1");   
  
  else if ($target. Is ("a.a2")
   {
   alert ("a.a2");   
  }
  else if ($target. Is (" A.uhead "))//www.111cn.net
  {
   alert (" A.uhead ");
  }
 } ;
});

For example, if we were to develop a dynamically added text box, there would be a label for each text box to cancel the text box. A help like this is inefficient:

The code is as follows Copy Code

$ (' #myList a). bind (' click ', function () {
$ (this). Closest (' Li '). Remove ();
Do stuff
});

Instead, we should listen for the Click event at the parent level.

The code is as follows Copy Code

$ (' #myList '). Bind (' click ', Function (e) {
var target = E.target,//E.target grabs the node that triggered the event.
$target = $ (target); Wraps the node in a JQuery object
if (Target.nodename = = ' A ') {
$target. Closest (' Li '). Remove ();
Do stuff Www.111cn.net
}
});

The parent node acts as a transmitter and can do some work on the target element that triggered the event

Example 5.10. Using $.fn.delegate delegate Events

The code is as follows Copy Code

$ (' #myUnorderedList '). Delegate (' Li ', ' click '), function (e) {
var $myListItem = $ (this);
// ...
});

Example 5.11. Using $.fn.live delegate Events

The code is as follows Copy Code

$ (' #myUnorderedList Li '). Live (' click ', Function (e) {
var $myListItem = $ (this);
// ...
});

Unbind a delegate event
If you need to remove a delegated event, you cannot simply unbind it. Use $.fn.undelegate to Unbind events that use $.fn.delegate bindings, and use $.fn.die to unbind events that use $.fn.live bindings. Like bindings, you can optionally unbind the name of the binding function.

  code is as follows copy code

$ (' #myUnordered List '). Undelegate (' Li ', ' click ');
$ (' #myUnorderedList Li '). Die (' click ');

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.