jQuery 文档翻译 .on()

.on( events [, selector ] [, data ], handler )

Description: Attach an event handler function for one or more events to the selected elements.

绑定一个事件处理函数到选定元素

 

The .on() method attaches event handlers to the currently selected set of elements in the jQuery object. As of jQuery 1.7, the.on() method provides all functionality required for attaching event handlers. For help in converting from older jQuery event methods, see .bind().delegate(), and .live(). To remove events bound with .on(), see .off(). To attach an event that runs only once and then removes itself, see .one()

 

.on()方法将事件处理函数绑定到当前选定的一系列元素上。

 

Event names and namespaces

Any event names can be used for the events argument. jQuery will pass through the browser‘s standard JavaScript event types, calling the handler function when the browser generates events due to user actions such as click. In addition, the .trigger() method can trigger both standard browser event names and custom event names to call attached handlers. Event names should only contain alphanumerics, underscore, and colon chraracters.

事件名称和命名空间

events参数可以是任意事件的名称。当浏览器由于用户操作产生事件时,jQuery将调用handler函数。

 

$( "p" ).on( "click", function() {
  alert( $( this ).text() );
});

  

function myHandler( event ) {
  alert( event.data.foo );
}
$( "p" ).on( "click", { foo: "bar" }, myHandler );

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。