How jquery uses an automatic trigger event Trigger_jquery

Source: Internet
Author: User

Sometimes, you need to simulate user actions to achieve the effect of clicking, for example, after the user enters the page
Triggers the Click event without active clicking.

For example, the following code:

<body>
  <a href= "#" onclick= "Javascript:document.getElementById (' d '). Innerhtml= ' X1 '" > Click 1</a >
  <a href= "#" onclick= "Javascript:document.getElementById (' d '). Innerhtml= ' x2 '" > Click 2</a>
  <a href= "#" onclick= "Javascript:document.getElementById (' d '). Innerhtml= ' x3 '" > click 3</a>
  </br>
  <span id= "D" ></span>
</body>

The effect of the ' Click 1 ' page will show X1, ' Click 2 ' page to show X2 ...
But need to implement first entry page for default is ' click 1 '

In jquery, you can use the trigger () method to complete the impersonation operation.
For example, you can use the following code to trigger the click event of a link.

$ (' a '). Trigger ("click");

This way, when the page is loaded, the first a-link Click event is triggered, and the page displays the X1

Triggering custom Events

The trigger () method can not only trigger events with the same name as supported by the browser, but also events that trigger custom names.
For example, to bind an "MyEvent" event to an element, the jquery code is as follows:

$ (' #btn '). Bind ("MyEvent", function () { 
  alert (' Custom event ');
});

To trigger this event, you can use the following code to implement this:

$ (' #btn '). Trigger ("MyEvent");

Passing Data

The trigger (Type,[data]) method has two parameters,
The 1th one is the event object or the type of event to be triggered.
The 2nd parameter is the additional argument passed to the event handler,
passed as an array. You can usually pass a parameter to the
callback function to distinguish whether this event was triggered by code or by a user.

The following is an example of passing data.

$ (' #btn '). Bind ("MyEvent", function (event,message1,message2) { 
  alert (message1 + "," + Message2);
});
$ (' #btn '). Trigger ("MyEvent", ["Hello", "world!"]);

The above is jquery automatic trigger Event trigger use method, hope to be helpful to everybody's study.

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.