JQuery auto trigger event trigger usage
Sometimes, you need to simulate user operations to achieve the click effect. For example, after a user enters the page
The click event is triggered without the need to actively click.
For example, the following code:
Click 1 click 2 click 3
The effect is that x1 is displayed on the 'click 1' page, and x2 is displayed on the 'click 2' page...
However, the first time you enter the page, the default value is 'click 1'
In jQuery, you can use the trigger () method to complete the simulation operation.
For example, you can use the following code to trigger the click Event of A link.
$('a').first().trigger("click");
In this way, when the page is loaded, the click event of the first A link is triggered, and the page displays x1
Trigger custom events
The trigger () method triggers events with the same name and custom names supported by the browser.
For example, if you bind an "myEvent" event to an element, the jQuery code is as follows:
$ ('# Btn'). bind ("myEvent", function () {alert ("Custom Event ");});
To trigger this event, use the following code:
$('#btn').trigger("myEvent");
Transmit data
The trigger (type, [data]) method has two parameters,
1st are event objects or event types to be triggered,
The 2nd parameters are additional parameters passed to the event handler function and passed in arrays. Generally, you can pass a parameter
The callback function is used to identify whether the event is triggered by code or by the user.
The following is an example of data transmission.
$('#btn').bind("myEvent", function(event,message1,message2){ alert(message1 + "," + message2);});$('#btn').trigger("myEvent", ["Hello","World!"]);