A simple description of the binding, triggering, and listening methods of jquery events _jquery

Source: Internet
Author: User
Tags bind event listener

If you are writing an article or Demo, for simplicity, you can of course use the event listener function and the methods provided by those event objects. But in practice, there are some methods and properties that have compatibility issues, so we use jQuery to eliminate compatibility issues.

Here's a quick look at the basics of the events in JQuery.

Binding events and Event agents

In jQuery, syntax sugars such as Click () are provided to bind the corresponding events, but it is recommended that the uniform use on () to bind events. Grammar:

. On (events [, selector] [, data], handler)

Events is the name of the event, you can pass the second parameter to implement the event agent, specific document. On () no more details here.

Event object that handles compatibility

There are also compatibility differences among event objects, such as JQuery, which encapsulates them and provides the same name as the standard.

If you want to access the original event object in the JQuery event callback function, you need to use event.originalevent, which points to the native event object.

Trigger Event Trigger Method

Clicking on a node that is bound to the Click event naturally triggers the Click event of the node to perform the corresponding callback function.

Trigger method can simulate triggering event, we click another node ELEMENTB, we can use:

$ (ELEMENTB). On (' click ', Function () {
 $ (Elementa). Trigger ("click");
});

The

event listens for event sniffing in
jQuery, and can be used for addeventlistener/attachevent simulations, respectively, for modern browsers and IE, to encapsulate two methods, but for convenience, Here the other event-related processing, such as the removal of event listening, blocking the default event, and other methods of unified writing in an object, easy to invoke, the specific code is as follows:

Event Processing Object var eventutil = {//Add Event listener add:function (element, type, callback) {if (Element.addeventlistener) {
  Element.addeventlistener (Type, callback, false);
  else if (element.attachevent) {element.attachevent (' on ' + type, callback);
  else {element[' on ' + type] = callback; },//Remove Event listener remove:function (element, type, callback) {if (Element.removeeventlistener) {Element.removeeve
  Ntlistener (Type, callback, false);
  else if (element.detachevent) {element.detachevent (' on ' + type, callback);
  else {element[' on ' + type] = NULL;
 ///cross-browser Get event object Getevent:function (event) {return event event:window.event;
 },//cross-browser Get target property gettarget:function (event) {return Event.target | | event.srcelement;
  },//block the default behavior of events Preventdefault:function (event) {if (Event.preventdefault) {event.preventdefault ();
  else {event.returnvalue = false; },//block event flow or use cancelbubble Stoppropagation:fuNction () {if (event.stoppropagation) {event.stoppropagation ();
  else {event.cancelbubble = true;
 
}
 }
 
};
Use example var at = document.getElementById (' atemp ');
 
 Eventutil.add (AT, ' click ', Function () {Console.log (' clicked on '); event = Eventutil.getevent (event); Cross-browser to get Event object Eventutil.preventdefault (event);
 Block default event});

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.