Usage Analysis of trigger () and bind () in jQuery _ jquery

Source: Internet
Author: User
This article mainly introduces the usage of trigger () and bind () in jQuery, and analyzes the functions, definitions, and usage techniques of trigger () and bind () in combination with examples, for more information about how to use trigger () and bind () in jQuery, see the following example. We will share this with you for your reference. The details are as follows:

Trigger (type)

Trigger an event on each matching element.

Returned value: jQuery

Parameters:

Type (String): the type of the event to be triggered.

Example:

The Code is as follows:

$ ("P"). trigger ("click ")

1. trigger () trigger event

This method is a newly added function in jQuery 1.3 that triggers the event.
The event here is like the event column in The jQuery help document, such as click, mouseover, keydown, and other js events with actions. For example, show and hide are not events.

2. Why use trigger ()?

I believe that this idea has been applied to everyone at the beginning?

For example, the front-end page contains:

Click here!


You want to execute this event when loading the page to bind the click event to this p (write the following code in $ (function ):

$("#p1").click(function(){  alert("hello!");});

If you use trigger (), you must write it as follows:

$("#p1").click(function(){  alert("hello!");}).trigger(click);

Isn't writing like this more troublesome? This 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 as follows:

$("#p1").bind("myEvent",function(event,str1,str2) {  alert(str1 + ' ' + str2);}).trigger("myEvent",["Hello","World"]);

I hope this article will help you with jQuery programming.

Related Article

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.