Analysis of noteworthy trigger methods in jQuery

Source: Internet
Author: User
In front-end page development, we sometimes want to customize some user-friendly operations, such as the mouse can be clicked automatically without clicking, the traditional js language requires complex code to implement this function. At this time, we can easily implement it using the jquery trigger () method. This article gives you a detailed introduction to the noteworthy trigger methods in jQuery. Introduction

The trigger method triggers an event of the specified type on the selected element. The syntax format of the trigger method is: trigger (type, [data]). the parameter type is the type of the trigger event, and the parameter data is optional, indicating the attachment parameter passed to the function when the event is triggered.

Common Simulation

Sometimes, you do not need to perform operations, but also want to simulate user operations to achieve some results. For example, the click event is triggered after the user enters the interface, without the user clicking.
Trigger can be used in jquery.

$ ("# Btn"). trigger ("click") // trigger the click event with the id btn $ ("# btn"). click () // abbreviation

Trigger custom events

Trigger not only triggers events supported by the browser, but also triggers custom events. For example, bind an event named clickMe:

$ ("# Btn "). bind ("clickMe", function (){//....}) $ ("# btn "). trigger ("clickMe") // triggers this event

Transmit data

trigger(type,[data])

The first parameter refers to the event type to be triggered, and the second parameter refers to the additional data to be passed to the event processing function, which is passed as an array. Generally, you can pass a parameter to the callback function to determine whether the event is triggered by the user.

Button

Script $ (function () {$ ('# btn '). bind ("clickMe", function (event, msg1, msg2) {$ ("# msg "). text (msg1 + ''+ msg2)}) $ ('# btn '). trigger ("clickMe", ["hello", "jquery"])}) script

When the trigger () method triggers an event, the default browser operation is performed. For example

$('input').trigger('focus')

The above code triggers not only the focus event bound to the input, but also the default focus event in the browser to get the focus. If you only want to trigger a custom focus event, use triggerHandler ()

$('input').triggerHandler('focus')

This method will only trigger events bound to the input, and cancel the default operations on this event by the browser, without getting the focus.

Summary

Well, the above is all about this article. I hope this article will help you in your study or work. If you have any questions, please leave a message.

For more articles about the noteworthy trigger method in jQuery, please pay attention to PHP!

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.