Home > Web Front-end > JS Tutorial > body text

Experience using trigger() & bind() in jQuery

巴扎黑
Release: 2017-06-25 13:15:53
Original
1472 people have browsed it

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template