Methods for automatically triggering events in jquery _jquery

Source: Internet
Author: User

This example describes how jquery automatically triggers events. Share to everyone for your reference. Specifically as follows:

Common analog

Sometimes, you need to simulate user actions to achieve the effect of a click. For example, when a user enters a page, it triggers the Click event without the user having to actively click.

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 for a button with an ID of btn.

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

This way, when the page is loaded, the desired effect is immediately output. You can also directly abbreviate click () to achieve the same effect:

$ (' #btn '). Click ();

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 "Myclick" event to an element, the jquery code is as follows:

$ (' #btn '). Bind ("Myclick", function () { 
  $ (' #test '). Append ("<p> My Custom Event .</p>"); 
});

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

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

Passing Data

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

The following is an example of passing data.

$ (function () { 
 $ (' #btn '). Bind ("Myclick", Function (event, Message1, Message2) { 
     $ (' #test '). Append ("<p> "+message1 + message2 + </p>"); 
 $ (' #btn '). Click (function () { 
  $ (this). Trigger ("Myclick", ["My Custom", "event"]) 
 . Trigger ("Myclick", ["My Customizations", "events"]); 
}

Perform the default action

After the trigger () method triggers the event, the browser default action is performed. For example:

$ ("input"). Trigger ("focus");

The above code not only triggers the focus event that is bound for the <input> element, but also causes the <input> element itself to be focused (this is the browser's default action).

If you want to trigger only the bound focus event and do not want to perform the browser default action, you can use another similar method in jquery to--triggerhandler () the method.

$ ("input"). Triggerhandler ("Focus");

This method triggers a specific event that is bound by the <input> element and cancels the browser's default action for this event, that is, the text box triggers only the binding focus event and does not get focused.

I hope this article will help you with your jquery programming.

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.