Trigger () and bind () usage analysis in jquery _jquery

Source: Internet
Author: User

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.

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.