jQuery triggerHandler()Method

jQuery 事件方法jQuery Event Methods

Example

Trigger the select event of the <input> element:

$("button").click(function(){ $("input").triggerHandler("select"); });

Try it Yourself »

Definition and Usage

The triggerHandler() method triggers the specified event on the selected element.

This method returns the return value of the event handler function, rather than a chainable jQuery object. In addition, if no handler is triggered, this method returns undefined.

This method is similar to thetrigger()method, the difference being that trigger() also triggers the default behavior of the event (like form submission).

Differences compared to the trigger() method:

  • It does not cause the default behavior of the event (such as form submission)
  • .trigger() operates on all elements matched by the jQuery object, while .triggerHandler() only affects the first matched element.
  • Events created by .triggerHandler() do not bubble up the DOM tree; if the target element does not directly handle them, nothing will happen.

Example

Comparing triggerHandler() with .trigger():

$( "#old" ).click(function() { $( "input" ).trigger( "focus" ); }); $( "#new" ).click(function() { $( "input" ).triggerHandler( "focus" ); }); $( "input" ).focus(function() { $( "<span>Focused!</span>" ).appendTo( "body" ).fadeOut( 1000 ); });

Try it Yourself »

Syntax

$(selector).triggerHandler(event,param1,param2,...)

Parameter Description
event Required. Specifies the event to trigger on the specified element.
param1,param2,... Optional. Additional parameters passed to the event handler.
Additional parameters are especially useful for custom events.


实例

More Examples

Passing additional parameters to custom events
How to pass additional parameters to a custom event handler.

The difference between trigger() and triggerHandler()
Demonstrates the difference between trigger() and triggerHandler().


jQuery 事件方法jQuery Event Methods

Other Extensions