jQuery API 返回首页目录 | jQuery API 中英文对照版
one(type, data, fn)

one(type,data,fn)

为每一个匹配元素的特定事件(像click)绑定一个事件处理器函数。

在每个对象上,这个事件处理函数只会被执行一次。其他规则与bind()函数相同。

这个事件处理函数会接收到一个事件对象,可以通过它来阻止(浏览器)默认的行为。如果既想取消默认的行为,又想阻止事件起泡,这个事件处理函数必须返回false。 多数情况下,可以把事件处理器函数定义为匿名函数(见示例一)。

在不可能定义匿名函数的情况下,可以传递一个可选的数据对象作为第二个参数(而事件处理器函数则作为第三个参数),见示例二。

返回值:jQuery

参数:

  • type (String): 事件类型
  • data (Object): (可选) 作为event.data属性值传递给事件对象的额外数据对象
  • fn (Function): 绑定到每个匹配元素的事件上面的处理函数
 

示例:

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

HTML 代码:

<p>Hello</p>

结果:

alert("Hello")

 
one(type,data,fn)

Binds a handler to a particular event (like click) for each matched element. The handler is executed only once for each element. Otherwise, the same rules as described in bind() apply. The event handler is passed an event object that you can use to prevent default behaviour. To stop both default action and event bubbling, your handler has to return false.

In most cases, you can define your event handlers as anonymous functions (see first example). In cases where that is not possible, you can pass additional data as the second paramter (and the handler function as the third), see second example.

Return value: jQuery
Parameters:

  • type (String): An event type
  • data (Object): (optional) Additional data passed to the event handler as event.data
  • fn (Function): A function to bind to the event on each of the set of matched elements
 

Example:

 $("p").one("click", function(){
     alert( $(this).text() );   
});  
Before:
 <p>Hello</p>  
Result:
 alert("Hello")  
相关链接
asp之家 | jQuery官方网站 | jQuery中文网 | 电子书作者网站 | 电子书作者blog