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 );

 

posted @ 2015-03-19 16:41  bjsunhe  阅读(168)  评论(0编辑  收藏  举报