27. Jquery events and 27jquery events
Jquery events
In javascript, the event calling methods are onclick and onmouseover. In jquery, you do not need to write the previous on
Bind () method
Bind events to elements
$ ("# Id"). bind ("click", function () {// bind a click event for the id alert ("OK ");});
Events bound using the bind () method are multicast event delegates, that is, events that are redefined do not overwrite the previous event.
// Two events can be executed in sequence $ ("# id "). bind ("click", function () {alert ("1") ;}); $ ("# id "). bind ("click", function () {alert ("2 ")});
One () method
Bind one-time event
// Only one click operation will not be executed next time $ ("# id "). one ("click", function () {alert ("a"); return false ;});
Unbind () method
Delete events and unbind
$ ("# Id "). unbind (); // delete all events of the id $ ("# id "). unbind ("click"); // delete all click events of the id
Quick events
Basic Support for removing on from javascript events
Click () method
Click Event
$ ("# Id "). click (); // if the function body is not written, the event is triggered immediately $ ("# id "). click (function () {// This Is A click event });
Dbclick () method
Double-click event
$ ("# Id"). dbclick (); // trigger event $ ("# id"). dbclick (function () {// double-click and execute });
Focus () method
Focus
$ ("# Id"). focus (); // trigger $ ("# id"). focus (function () {// execute after obtaining focus });
Blur () method
No focus
$ ("# Id "). blur (); // triggers the event of losing focus $ ("# id "). blur (function () {// execute after the focus is lost });
Change () method
Change
$ ("# Id"). change (); // trigger the change event $ ("# id"). change (function () {// execute after change });
Mouseover () method
Move the mouse in
$("#id").mouseover();$("#id").mouseover(function(){});
Mouseout () method
Move the mouse out
$("#id").mouseout();$("#id").mouseout(function(){});
Submit () method
Submit Form
$("#id").submit(); $("#id").submit(function(){});
Hover () method
Used to solve the problems of mouseover and mouseout
// The two functions correspond to the moving and removing functions respectively $ ("div "). hover (function () {$ ("div" ).html ("ah He came") ;}, function () {$ ("div" ).html ("the call, scared to death, finally left! ")});
Toggle () method
Execute each function in sequence.
$("#div").toggle( function(){ $("#div").css({ "width": "100px" }); }, function(){ $("#div").css({ "width": "200px" }); }, function(){ $("#div").css({ "width": "300px" }); });
Jquery event object
Jquery eventmerges the differences between different browsers. For example, you can use event.tar get in a browser to obtain the trigger without judging whether the browser uses srcElement.
$ ("# Div "). click (function (e) {alert (e. type); // returns the event name click alert(e.tar get); // returns the alert (e. altKey); // returns whether alt is pressed by alert (e. screenX );//... events in js });