Home >Web Front-end >JS Tutorial >A brief analysis of the noteworthy trigger methods in jQuery

A brief analysis of the noteworthy trigger methods in jQuery

高洛峰
高洛峰Original
2016-12-28 10:16:051160browse

Introduction

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

Commonly used simulation

Sometimes, there is no need to perform operations, but also want to Simulate user operations to achieve certain effects. For example, the click event is triggered after the user enters the interface, without the user having to click.
It can be done using trigger in jquery.

$("#btn").trigger("click")//触发id为btn的click事件
 
$("#btn").click()//简写

Trigger custom events

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

$("#btn").bind("clickMe",function(){
 //....
})
 
$("#btn").trigger("clickMe")//触发该事件

Transfer data

trigger(type,[data])

The first parameter refers to the type of event triggered, and the second parameter is the additional data to be passed to the event processing function, passed in the form of an array. You can usually distinguish whether this event is triggered by the user by passing a parameter to the callback function.

<button id="btn">按钮</button>
<p id="msg"></p>
<script>
$(function(){
 $(&#39;#btn&#39;).bind("clickMe",function(event,msg1,msg2){
 $("#msg").text(msg1+&#39; &#39;+msg2)
 })
 $(&#39;#btn&#39;).trigger("clickMe",["hello","jquery"])
})
</script>

A brief analysis of the noteworthy trigger methods in jQuery

Effect screenshot

Perform default operation

## After the #trigger() method triggers an event, the browser's default operation will be performed. For example,

$(&#39;input&#39;).trigger(&#39;focus&#39;)

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

$(&#39;input&#39;).triggerHandler(&#39;focus&#39;)

This method will only trigger the event bound to the input and cancel the browser's default operation for this event. Will not get focus.

Summary

Okay, the above is the entire content of this article. I hope the content of this article can bring some help to everyone's study or work. If you have any questions, you can leave a message to communicate. .

For more relevant articles on the noteworthy trigger methods in jQuery, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn