This article will share with you my understanding about events in jQuery. It mainly introduces events binding, merging, event bubbling, and event blocking in jQuery, I hope you will like it.
JQuery event first
1. Event binding in JQuery
In fact, the standard event binding in JQuery is written as follows: (as follows)
The Code is as follows:
$ ("# Btn"). bind ("click", function (){});
But every time I Write this, it's too much trouble. How can I Write less and Do more?
Therefore, we are used to abbreviated it.
$ ("# Btn"). click (function () {}) makes it much easier
2. Combine event hover (enterfn, leavefn)
Call the enterfn method when you place the cursor over the element,
Call the leavefn method when the mouse leaves the element.
It is equivalent to the combination of mouseover and mouseout events in javascript.
Event bubbling
1. Description
Event bubbling: JQuery uses the event bubbling mechanism like JavaScript.
, Window. event. cancelBubble = true
2. Get
If you want to capture event-related information, add the parameter e to the response's anonymous function. e is the event object.
Call the stopPropagation () method of the event object to terminate the bubble.
E. stopPropagation ();
After the bubble is terminated, the program will not run on the parent element of the event source.
3. Event bubble chart
Block events
Block default behavior: some elements have default behavior. For example, after a hyperlink is clicked, the new link is switched and the submit button is used to submit a form by default. To prevent default behavior, you only need to call the preventDefault () of the event object () method and window. event. returnValue = false has the same effect.
The Code is as follows:
$ ("A"). click (function (e ){
Alert ("All hyperlinks are temporarily prohibited from clicking ");
E. preventDefault ();
});
The above is a full description of jQuery events in this article, hoping to help you learn jQuery.