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.