Skip to content


jQuery学习笔记7(event)

本篇主要记录jQuery事件响应的处理

50. bind(type, data, fn)对所有选定的element判断事件,type是事件类型,data是参数,fn是事处理方法.

eg:1. $(”p”).bind(”click”, function(){ alert( $(this).text() ); });

2. function handler(event) { alert(event.data.foo); }

$(”p”).bind(”click”, {foo: “bar”}, handler)==>alert(”bar”)

51. blur() 触发blur方法,blur(fn)设置blur函数

eg: <p>Hello</p>

$(”p”).blur( function() { alert(”Hello”); } );==>

<p onblur=”alert(’Hello’);”>Hello</p>

52. change(fn)设置onchange事件的方法

eg:<p>Hello</p>

$(”p”).change( function() { alert(”Hello”); } );==><p onchange=”alert(’Hello’);”>Hello</p>

53 click()触发onclick事件, click(fn)设置onclick方法

54. dblclick(fn)设置ondblclick方法

55.error(fn)设置error方法

56 focus()触发onfocus,focus(fn)设置focus方法

eg:<p>Hello</p>

$(”p”).focus( function() { alert(”Hello”); } );

==><p onfocus=”alert(’Hello’);”>Hello</p>

57 hover(over, out) 设置mouse移入以及mouse移出时所触发的事件.

eg: $("p").hover(function(){
  $(this).addClass("over");
},function(){
  $(this).addClass("out");
});
58 keydown(fn),keypress(fn),keyup(fn),按键盘相关的操作分别对应onkeydown,
onkeypress,onkeyup.
59 mousedown(fn),mousemove(fn),mouseout(fn),mouseover(fn),mouseup(fn)这些是mouse
相关的操作分别对应onmousedown,onmousemove,onmouseout,onmouseover,onmouseup.
60 load 当element load事件发生时触发
eg <p>Hello</p>
$("p").load( function() { alert("Hello"); } );==>
<p onload="alert('Hello');">Hello</p>
61.one(type, data, fn)与bind差不多,最大的区别是前者只运行一次后便不再响应.如果运行后
不想继续运行默认的方法只要fn中返回值return false就可以了.
eg: <p>Hello</p>
$("p").one("click", function(){ alert( $(this).text() ); });==>alert("Hello")
62.ready(fn)当dom 结构载入完毕的时候.用此方法时,请不要在onload方法里面写代码.不然就不
会触发ready事件
eg .1.$(document).ready(function(){ Your code here... });
2.jQuery(function($) {
  // Your code using failsafe $ alias here...
});
63.resize 设置onresize方法.当大小改变时触发
eg: <p>Hello</p>
$("p").resize( function() { alert("Hello"); } );
<p onresize="alert('Hello');">Hello</p>
64 scroll(fn) 设置onscroll方法
65.select()触发onselect方法.select(fn)设置select方法
66. submit()提交表单,submit(fn)设置submit方法.
eg: $("#myform").submit( function() {
return $("input", this).val().length > 0; } );
<form id="myform"><input /></form>
67 toggle(even, odd),even当偶数单击时运行,odd当奇数单击时运行.用unbind('click')删除
eg: $("p").toggle(function(){
  $(this).addClass("selected");
},function(){
  $(this).removeClass("selected");
});
68 trigger(type)触发相应的type方法
eg: <p click="alert('hello')">Hello</p>
$("p").trigger("click")==>alert('hello')
69 unbind(type, fn)与bind相反,
1.unbind()删除所有的绑定.
eg:<p onclick="alert('Hello');">Hello</p>
$("p").unbind()==>[ <p>Hello</p> ]
2.unbind(type)删除指定的绑定
eg:<p onclick="alert('Hello');">Hello</p>
$("p").unbind( "click" )==>[ <p>Hello</p> ]
3.unbind(type, fn)删除指定type的指定fn
eg:<p onclick="alert('Hello');">Hello</p>
$("p").unbind( "click", function() { alert("Hello"); } )
==>[ <p>Hello</p> ]
70 unload(fn)当页面unload的时候触发方法
eg: <p>Hello</p>
$("p").unload( function() { alert("Hello"); } );==>
<p onunload="alert('Hello');">Hello</p>






		

Posted in jQuery, 技术.

Tagged with , .


One Response

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. DBme says

    “52. change(fn)设置onchange事件的方法”
    这个例子有问题,change事件会在元素失去焦点的时候触发,也会当其值在获得焦点后改变时触发,一个

    标签怎么获得焦点呢?



Some HTML is OK

or, reply to this post via trackback.