How jQuery uses auto-trigger event trigger_jquery-js tutorial

Source: Internet
Author: User
This article describes how to use jQuery to automatically trigger an event trigger. For more information, see. 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,
Transmitted as an array. 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!"]);

The above describes how to use jQuery to automatically trigger an event trigger. I hope this will be helpful for your learning.

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.