An event is an interaction between JavaScript and HTML that is raised when a page or page is loaded when a user operates. Common Elements Click events, mouse events, keyboard input events, and so on, compared to the JavaScript
The ability to handle events is greatly enhanced by the addition of jquery and the expansion of basic event-handling mechanisms.
One, Dom load events
When the page is loaded, the browser loads the event via JavaScript for the DOM element, using JavaScript when the Window.onload method is used, and jquery uses $ (document). Ready () method, the following table
Show the similarities and differences of two events.
|
Window.onload method |
$ (document). Ready () method |
| Timing of execution |
You must wait for all content to load to complete before executing |
When a page's DOM element is drawn, it can be done without waiting for the full contents to load. |
| Number of bound functions |
This method is capable of binding a function, if the binding multiple can perform the last binding. Example:
Window.onload=function () {alert ("1");}
Window.onload=function () {alert ("2");}
Execution results only the last bound function results are printed: 2. |
Ability to register multiple functions and perform binding functions in binding order. Cases:
$ (document). Ready (Funtction () {alert ("1");});
$ (). Ready (function () {alert ("2");});
The result is a function that performs the binding sequentially: Print 1 First, then Print 2. |
| Simplified wording |
No |
$ (). Ready (); $.ready () |
Ii. Handling of events
When the file is loaded, you can add event handling for the element, and then the event performs some action to complete the operation. jquery has a variety of event handling methods, mainly bind (), one (), Unbind (), Live (), Trigger (), Triggerhander (), and so on. The jquery method uses the Bind () method to make specific event bindings for the matching elements. The bind () method is formatted as follows:
Bind (type,[data],fun);
The bind () method has three parameters, and each parameter is described as follows:
The first parameter is the event type, mainly as follows: Blur, focus, load, resize, scroll, unload, click, Dbclick, MouseDown, MouseUp, MouseMove, MouseOver, Mouseout, MouseEnter, MouseLeave, change, select, Submit, KeyDown, KeyPress, KeyUp and error. It can also be a custom name, and the event-binding type in jquery is less "on" than JavaScript, so the event click in jquery corresponds to the onclick function in JavaScript.
The second parameter is an optional parameter that is passed as an additional data object to the event object as the Event.data property value.
The third parameter is the processing function that is used to bind.
For button-binding Click event Example:
<input type= "button" id= "BTN1" value= "click" >
$ ("#btn1"). Bind ("click", Function () {alert ("clicking Event Binding");});
The bind () method is valid for an element-bound event until the Unbind () method or the element is deleted. If you want to bind an event only once, you must bind the event using the one () method. One () is the same as the bind () method, except that an event that is bound by a () method can only be executed once.
Example of a one-time event for a button binding:
<input type= "button" id= "BTN1" value= "click" >
$ ("#btn1"). One (' click ', function () {Alert ("click event Bindings");
The unbind () method cancels the event binding for the element, and the unbind () method is formatted as follows:
$ (selctor). Unbind ([Type],[data])
The unbind () method is the reverse operation of the element bind () method, which deletes the bound event from each matching element. Deletes all binding events when there are no parameters. Deletes the specified binding event if a parameter with an event type is provided. If the event type is specified, the second parameter specifies the function name to delete the event in the event type that corresponds to the second parameter name.
Example:
<input type= "button" id= "BTN1" value= "click"/> <input "
button" type= "id=" btn2 "value=" Removebind
$ ("#btn1"). Bind ("click", fun1 = function () {alert ("Event 1");});
$ ("#btn1"). Bind ("click", fun2 = function () {Alert ("Event 2");});
$ ("#btn2"). Bind ("click", Function () {
$ ("#btn1"). Unbind ("click", fun2);
The example removes the second-bound event-handler function using the two-parameter form of the Unbind () method.
The live () method binds the event dynamically, and when the method is used to bind the event, the new addition of the corresponding element can trigger the corresponding event. The live () method is formatted as follows:
$ (selector). Live ([Type],[data],fun);
Live Dynamic Binding Example:
$ ("P"). Live ("Click", Function () {$ (this). Hide ();});
The example dynamically adds a click event for all P-element nodes, either an existing P-element node or a P-element node that is to be added later, which triggers the hide () to hide itself. The die () method is the reverse operation of the Live () method, with the result that the live () method dynamically binds the event and the Unbind () method. When dynamic binding is used with the Die () method, all dynamically bound events are deleted.
The trigger () method triggers an event in a parameter on a matching element that triggers the browser default event, and returns false to prevent the browser from default events. The trigger () method is formatted as follows:
$ (selector). Trigger ([Type],[data]);
Trigger () Method Example:
$ ("Form:first"). Trigger ("submit");
This example triggers a submit operation for the first from form.
Triggerhandler () method This method triggers the binding event, but does not trigger the browser default event. Returns false effects when triggered by the trigger () method. Use the same method as trigger.
III. Events
The main event types listed in event handling are as follows: Blur, focus, load, resize, scroll, unload, click, Dbclick, MouseDown, MouseUp, MouseMove, MouseOver, Mouseout, MouseEnter, MouseLeave, change, select, Submit, KeyDown, KeyPress, KeyUp and error. The individual event types are mainly two usage binding events, triggering events. Take the Click event as an example, the click Method is formatted as follows: Bind event $ (selector). Click (Functiron () {//function body}), triggering event $ (selector). Click (). Cases:
<input type= "button" id= "BTN1" value= "binding event"/> <input "
button" type= "id=" BTN2 "trigger event" value=
$ ("# Btn1 "). Click (function () {alert (" event binding ");});
$ ("#btn2"). Click (function () {$ (#btn1). click ();})
The example btn1 the binding event for the first button, triggering the binding event of the first button in the binding event for the button BTN2 binding event. The remaining event types are similar to click.
Iv. Synthetic Events
A composite event can also be called an interaction event is a jquery customization method with two hover () and toggle ().
The hover () method mimics the mouse hover event in the following way: hover (enter,leave). Cases:
$ ("ul Li"). Hover (function () {alert ("Enter");},functiron () {alert ("Leave");});
The example for the UL Li element binding hover event, when the mouse stops on the UL element Li will trigger the ENTER () function, print "Enter", when the mouse moved out LI element will print "leave".
The toggle () method Toggles the execution of the bound function by looping the method in the following way: Toggle (fun1,fun2,..., Funn);
<input type= "button" id= "BTN1" value= "trigger switch operation"/>
$ ("#btn1"). Toggle (function () {alert (start);},function () {alert ("continue");},function () {alert ("End");});
This example triggers one of the binding events in sequential order for button BTN1 binding toggle events each click. The example first clicks print "Start", the second click Print "Continue", the third click print "End", click again and the first time the same print "start", circular sequence execution function.
V. Event Properties
jquery encapsulates event properties so that event handling does not require a discriminant type to work correctly under each browser. The individual event properties are as follows:
Event Type Event.type. This property is used to obtain the type that triggers the event, and returns different results for the different operations that do not raise the event;
$ ("P"). Click (Function (event) {alert (event.type);});
The example returns the event type of the element p click event, and the result print is "click";
Event Type Event.target This property to get the element that triggers the event;
$ ("a"). Click (Function (event) {alert (event.target.href);});
The example returns the href attribute of element A.
Event default Behavior Event.preventdefault This property is used to block the default properties of an event, for example:
$ ("a"). Click (Function (event) {event.preventdefault;});
Use this property to block the default jump behavior of element A, and the result is the same as return false.
The event bubbling Event.stroppropagation property is used to block event bubbling, for example:
$ ("div"). Click (Function (event) {alert ("innermost click"); event.stoppropagation;};
The example uses the Stoppropagation property to prevent the click event of a div's parent element from being triggered.
The Event.pagex and Event.pagey properties, which enable you to obtain the mouse's current position relative to the page. Cases:
$ ("P"). Click (Function (event) {alert ("Current mouse location" +event.pagex+ "+event.pagey);});
The example gets the current position of the mouse when clicking on the element p and prints it out.
Event.witch property, which allows you to obtain the key value in the keyboard event by using the left and right mouse button when the mouse clicks.
$ ("P"). MouseDown (function (event) {alert (event.witch);});
This example prints the mouse button value when the mouse is pressed. The left middle right is 1, 2, 3 respectively.
The Event.metakey property is used to obtain the CTRL key for the keyboard in an event:
$ ("Body"). KeyDown (function (event) {if (Event.metakey) {alert ("Ctrl");} Else{alert ("No Ctrl");});
The example prints CTRL if the key is pressed as the CTRL key when you press the keyboard on the page, otherwise print "No Ctrl".
VI. Event bubbling
There are multiple binding events in the page so it is possible for multiple elements to respond to the same event, which causes events that are not what we expected, which is event bubbling, and to deal with this problem can limit the scope of the event, and jquery makes Stoppropagation () method to stop the bubbling of an event. Event Bubbling Example:
<body><div><span> event bubbling </span>div content </div>body </body>
$ ("Body"). Click ( function () {alert ("Body");});
$ ("Body div"). Click (function () {alert ("div");});
$ ("span"). Click (function () {alert ("span");});
In this example, the three print events "span" div "body" are triggered when you click Span, how to prevent event bubbling? You can use the Stroppropagation () method in the jquery event to stop event bubbling. The example above changes to this:
$ ("Body"). Click (Function (event) {alert (' body '; event.stoppropagation ();});
$ ("Body div"). Click (Function (event) {alert ("div"); event.stoppropagation ();});
$ ("span"). Click (Function (event) {alert (' span '); event.stoppropagation ();});
This does not happen when you click on different elements.
Vii. Event Namespaces
Events add namespaces to facilitate the management of events: deleting events and triggering events. Cases:
<div> element Binding Multiple Events </div>
$ ("div"). Bind ("Click.plugin", function () {alert ("Click.plugin");});
$ ("div"). Bind ("click", Function () {Alert ("click");});
$ ("div"). Bind ("Mouseover.plugin", function () {alert ("Mouseover.plugin");});
$ ("div"). Bind ("Dbclick", function () {alert ("Dbclick");});
Because namespaces can be used to delete events in the specified namespace, such as removing the event function methods under all plugin namespaces are as follows: $ ("button"). Click (function () {$ ("div"). Unbind (". Plugin");}); This removes all the namespaces that are plugin event functions. Only the Click and Dbclick binding events are left in the Div.
Using namespaces is convenient for triggering specific events, such as binding two click events for a Div, one with a namespace and without a namespace. If you want to trigger an event that is not contained in a namespace, you can use trigger ("click!") Method! The effect of an exclamation point is to match all click methods that are not contained in the namespace.
The above jquery basis of the incident operation is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.