Home > Web Front-end > JS Tutorial > Detailed explanation of jQuery.trigger() function

Detailed explanation of jQuery.trigger() function

巴扎黑
Release: 2017-07-08 11:13:28
Original
2438 people have browsed it

trigger() function is used to trigger an event of the specified type on each matching element.

In addition, you can also pass in additional parameters to the event handling function when the event is triggered.

Using this function can manually trigger the execution of the event processing function bound to the element, and will also trigger the execution of the default behavior of the element.

Taking the form element

as an example, using trigger("submit") can trigger the submit event bound to the form, and also execute the default behavior of the form submit event - the form submission operation.

According to feedback from netizen @Feiyang, the trigger("click") of the link tag is a special case and will not trigger the default behavior of the link click event - jump to the operation of the corresponding link, click View related details here.

Starting from jQuery 1.3, events triggered by the trigger() function also support event bubbling, which can be bubbled and passed to the DOM tree.

This function belongs to the jQuery object (instance).

Syntax

The trigger() function mainly has the following two forms of usage:

Usage 1:

jQueryObject.trigger( events [, extraArguments ] )

Trigger events of the specified type (events) on each matching element, and can pass in additional parameters (extraArguments) to the event handling function.

Usage 2: jQuery 1.3 newly supports this usage.

jQueryObject.trigger( eventObject [, extraArguments ] )

is the Event object (eventObject) passed in by the specified event processing function, which is used to trigger the execution of the corresponding event processing function and can be used for events The processing function passes in extra parameters (extraArguments).

Parameters

Parameter Description

events String type specifies the event type and optional namespace, such as "click", "focus", "keydown.myPlugin".

extraArguments Optional/Object type is the extra parameters passed in by the event processing function. If you want to pass in multiple parameters, please pass them in as an array.

eventObject Object type is an Event object, used to trigger the event processing function passed in to the object.

Thetrigger() function will pass in a default parameter for the event processing function that triggers execution, which is the Event object representing the current event.

The parameter extraArguments is used to pass in more additional parameters to the event handling function. If extraArguments is in array form, each element will serve as a parameter to the function.

Return value

trigger()The return value of the function is of jQuery type and returns the current jQuery object itself.

Example & Description

Please refer to the following initial HTML code:

<input id="btn1" type="button" value="点击1" />
<input id="btn2" type="button" value="点击2" />
<a id="a1" href="#">CodePlayer</a>
<div id="log"></div>
Copy after login

First, we bind events to the above button and elements, and then use trigger () function directly triggers events, and the corresponding code is as follows:

trigger() function can also trigger corresponding events based on the Event object passed in to the event processing function.

In addition, the trigger() function can also trigger only events that contain a specified namespace (namespaces are only supported in 1.4.3+).

The above is the detailed content of Detailed explanation of jQuery.trigger() function. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template