Home > Article > Web Front-end > Experience using trigger() & bind() in jQuery
trigger(type)
Trigger a certain type of event on each matching element.
Return value:jQuery
Parameters:
type ( String): The type of event to be triggered
Example:
$("p").trigger(" click")
1.trigger() Trigger event
This method is a new function in jQuery 1.3 that causes a trigger event. The events here are just like the events column in the jQuery help document, such as click, mouseover, keydown and other js events with actions, while show and hide are effects and not events.
2. Why use trigger()?
I believe everyone who has just started to have this idea also has this idea?
For example, on the front page there is:
Please click here!
You want to execute this event when the page is loaded and bind a click event to this p (write the following code in $(function(){});) :
$("#p1").click(function(){
});
If you use trigger(), you have to write it like this:
").click(function(){
## alert("hello!");
}).trigger(click);
Wouldn’t it be more troublesome to write like this? It can be said that, but the biggest advantage of using trigger() is that it can pass parameters in. For example:
//myEvent is a custom event name## $("#p1").bind("myEvent",function(event,str1 ,str2) {
alert(str1 + ' ' + str2);
}); $("#p1").trigger("myEvent",["Hello","World"]);
You can also write like this:
$("#p1").bind("myEvent",function(event,str1,str2) {alert(str1 + ' ' + str2);
}).trigger("myEvent",["Hello","World"]);
The above is the detailed content of Experience using trigger() & bind() in jQuery. For more information, please follow other related articles on the PHP Chinese website!