ITeye博客

jQuery:Events函数的使用

jian0487 · 2009-08-11 00:10 · Jquery · json · jQuery · Markdown

函数:blur(),click(),focus(),select(),submit()

功能:这类函数主要是引发对象的相应事件

返回:jQuery对象

例子:

jQuery Code

$("p").click();

Before

<p onclick="alert('Hello');">Hello</p>

Result:

alert('Hello');



jQuery Code

$("form").submit();
函数:blur(fn),click(fn),dblclick(fn),error(fn),focus(fn),

      keydown(fn),keypress(fn),keyup(fn),load(fn),

      mousedown(fn),mouseover(fn),mousemove(fn),mouseup(fn),

      ready(fn),resize(fn),scrool(fn),select(fn),unload(fn)

功能:给匹配元素添加相应的事件

返回:jQuery对象

参数:要激发的事件响应函数

例子:

jQuery Code

$("p").click( function() { alert("Hello"); } );

Before

<p>Hello</p>

Result:

<p onclick="alert('Hello');">Hello</p>
函数:bind(type, data, fn)

功能:给匹配元素绑定事件的响应函数

参数:type事件类型名,data一个用来个响应函数传递参数的json对象,fn响应函数

例子:

jQuery Code

$("p").bind("click", function(){

  alert( $(this).text() );

});

Before

<p>Hello</p>

Result:

alert("Hello")



Pass some additional data to the event handler.



jQuery Code

function handler(event) {

  alert(event.data.foo);

}

$("p").bind("click", {foo: "bar"}, handler)

Result:

alert("bar")
函数:unbind(type, fn)

功能:解除事件的绑定

返回:jQuery对象

参数:type事件的类型名,fn响应函数

例子:

jQuery Code

$("p").unbind()

Before

<p onclick="alert('Hello');">Hello</p>

Result:

[ <p>Hello</p> ]



jQuery Code

$("p").unbind( "click" )

Before

<p onclick="alert('Hello');">Hello</p>

Result:

[ <p>Hello</p> ]
函数:one(type, data, fn) 

功能:只响应一次事件,类似bind(type,data,fn)
函数:hover(over, out)

功能:设定鼠标划入和划出匹配元素的事件

返回:jQuery对象

参数:

over (Function): The function to fire whenever the mouse is moved over a matched element. 

out (Function): The function to fire whenever the mouse is moved off of a matched element. 

例子:

jQuery Code

$("p").hover(function(){

  $(this).addClass("hover");

},function(){

  $(this).removeClass("hover");

});
函数:trigger(type, data)

功能:引发相应事件

返回:jQuery对象

参数:type事件类型名,data要传递给事件处理函数的可选参数

例子:

jQuery Code

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

Before

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

Result:

alert('hello')



Example of how to pass arbitrary data to an event



jQuery Code

$("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"]);



jQuery Code

$("p").bind("myEvent",function(event,message1,message2) {

    alert(message1 + ' ' + message2);

});

$("p").trigger("myEvent",["Hello","World"]);

Result:

alert('Hello World') // One for each paragraph
函数:toggle(even, odd)

功能:每次单击时切换事件处理

返回:jQuery对象

参数:

even (Function): The function to execute on every even click. 

odd (Function): The function to execute on every odd click. 

例子:

jQuery Code

$("p").toggle(function(){

  $(this).addClass("selected");

},function(){

  $(this).removeClass("selected");

});

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。