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.