jQuery API jQuery API 中英文对照版 - jQuery在线查询手册
trigger(type)
trigger(type)

在每一个匹配的元素上触发某类事件。

返回值:jQuery

参数:

  • type (String): 要触发的事件类型

示例:

$("p").trigger("click")

HTML 代码:

<p click="alert('hello')">Hello</p>

结果:

alert('hello')

 
trigger(type, data)

Trigger a type of event on every matched element. This will also cause the default action of the browser with the same name (if one exists) to be executed. For example, passing 'submit' to the trigger() function will also cause the browser to submit the form. This default action can be prevented by returning false from one of the functions bound to the event.

You can also trigger custom events registered with bind.

Return value: jQuery
Parameters:

  • type (String): An event type to trigger.
  • data (Array): (optional) Additional data to pass as arguments (after the event object) to the event handler

Example:

 $("p").trigger("click")  
Before:
 <p click="alert('hello')">Hello</p>  
Result:
 alert('hello')  

Example:

Example of how to pass arbitrary data to an event

 $("p").click(function(event, a, b) {
     // when a normal click fires, a and b are undefined 
    // for a trigger like below a refers too "foo" and b refers to "bar"   
}).trigger("click", ["foo", "bar"]);  
Example:
 $("p").bind("myEvent",function(event,message1,message2) {
   	alert(message1 + ' ' + message2); 
  });  
 $("p").trigger("myEvent",["Hello","World"]);  
Result:
 alert('Hello World') // One for each paragraph