This example describes the trigger () and bind () usage in jquery. Share to everyone for your reference, specific as follows:
Trigger (Type)
Triggers a class of events on each matching element.
return value: JQuery
Parameters:
Type (String): Types of events to trigger
Example:
Copy Code code as follows:
$ ("P"). Trigger ("click")
1.trigger () triggering event
This method is a new function in jquery 1.3 that triggers the event.
The events here are like the events in jquery's help documentation, such as: Click, MouseOver, KeyDown, and so on with the action of JS events, and like show, hide this is the effect is not an event.
2. Why use Trigger ()?
I believe that we have just begun to contact with this idea?
For example, the front page has: <p id= "P1" > please click here! </p>
You want to load the page to execute this event to this P bind the click event (write the following code in $ (function () {}); inside):
$ ("#p1"). Click (function () {
alert ("hello!");
});
If you use trigger (), you will write this:
$ ("#p1"). Click (function () {
alert (' hello! ');
}). Trigger (click);
Isn't it more troublesome to write this? It can be said, but the biggest advantage of using trigger () is that it can pass parameters in. For example:
MyEvent is the custom event name
$ ("#p1"). Bind ("MyEvent", function (EVENT,STR1,STR2) {
alert (str1 + ' + str2);
});
$ ("#p1"). Trigger ("MyEvent", ["Hello", "World"]);
You can also write this:
$ ("#p1"). Bind ("MyEvent", function (EVENT,STR1,STR2) {
alert (str1 + ' + str2);
}). Trigger ("MyEvent", ["Hello", "World"]);
I hope this article will help you with the jquery program design.