事件
页面载入
-
ready(fn)
当DOM载入就绪可以查询及操纵时绑定一个要执行的函数
$(document).ready(function(){
});
$(function($) {
});
事件处理
-
on(events,fn)
在选择元素上绑定一个或多个事件的事件处理函数
$("p").on("click", function(){
alert( $(this).text() );
});
$("p").click(function(){
alert( $(this).text() );
});
-
off(events,[fn])
在选择元素上移除一个或多个事件的事件处理函数
$("p").off()
$("p").off( "click")
-
bind(events,fn)
为每个匹配元素的特定事件绑定事件处理函数
$("p").bind("click", function(){
alert( $(this).text() );
});
$('#foo').bind('mouseenter mouseleave', function() {
alert();
});
-
unbind(type,fn]])
bind()的反向操作,从每一个匹配的元素中删除绑定的事件
$("p").unbind()
$("p").unbind( "click" )
-
one(type,[data],fn)
为每一个匹配元素的特定事件(像click)绑定一个一次性的事件处理函数
$("p").one("click", function(){
alert( $(this).text() );
});
事件委派
-
delegate(selector,[type],[data],fn)
指定的元素(属于被选元素的子元素)添加一个或多个事件处理程序,并规定当这些事件发生时运行的函数
<div style="background-color:red">
<p>这是一个段落。</p>
<button>请点击这里</button>
</div>
$("div").delegate("button", "click", function () {
$("p").slideToggle();
});

-
undelegate([selector,[type],fn])
删除由 delegate() 方法添加的一个或多个事件处理程序
$("p").undelegate();
$("p").undelegate( "click" )
事件切换
-
hover([over,]out)
一个模仿悬停事件(鼠标移动到一个对象上面及移出这个对象)的方法
over:鼠标移到元素上要触发的函数
out:鼠标移出元素要触发的函数
$("td").hover(
function () {
$(this).addClass("hover");
},
function () {
$(this).removeClass("hover");
}
);
事件
-
blur([[data],fn])
当元素失去焦点时触发 blur 事件
$("td").hover(
function () {
$(this).addClass("hover");
},
function () {
$(this).removeClass("hover");
}
);
-
change([[data],fn])
当元素的值发生改变时,会发生 change 事件
$(selector).change();
-
click([[data],fn])
触发每一个匹配元素的click事件
$("p").click();
-
dblclick([[data],fn])
当双击元素时,会发生 dblclick 事件
$("p").dblclick( function () { alert("Hello World!"); });
-
error([[data],fn])
当元素遇到错误(没有正确载入)时,发生 error 事件
$(window).error(function(msg, url, line){
jQuery.post("js_error_log.php", { msg: msg, url: url, line: line });
});
-
focus([[data],fn])
当元素获得焦点时,触发 focus 事件
$(document).ready(function(){
$("#login").focus();
});
-
focusin([data],fn)
当元素获得焦点时,触发 focusin 事件
<p><input type="text" /> <span>focusout fire</span></p>
<p><input type="password" /> <span>focusout fire</span></p>
$("p").focusin(function() {
$(this).find("span").css('display','inline').fadeOut(1000);
});
-
focusout([data],fn)
当元素失去焦点时触发 focusout 事件
$("p").focusout(function() {
$(this).find("span").css('display','inline').fadeOut(1000);
});
-
keydown([[data],fn])
当键盘或按钮被按下时,发生 keydown 事件
$(window).keydown(function(event){
switch(event.keyCode) {
}
});
-
keypress([[data],fn])
当键盘或按钮被按下时,发生 keypress 事件
$("input").keydown(function(){
$("span").text(i+=1);
});
-
keyup([[data],fn])
当按钮被松开时,发生 keyup 事件。它发生在当前获得焦点的元素上
$("input").keyup(function(){
$("input").css("background-color","#D6D6FF");
});
-
mousedown([[data],fn])
当鼠标指针移动到元素上方,并按下鼠标按键时,会发生 mousedown 事件
$("button").mousedown(function(){
$("p").slideToggle();
});
-
mouseenter([[data],fn])
当鼠标指针穿过元素时,会发生 mouseenter 事件
$("p").mouseenter(function(){
$("p").css("background-color","yellow");
});
-
mouseleave([[data],fn])
当鼠标指针离开元素时,会发生 mouseleave 事件
$("p").mouseleave(function(){
$("p").css("background-color","#E9E9E4");
});
-
mousemove([[data],fn])
当鼠标指针在指定的元素中移动时,就会发生 mousemove 事件
事件坐标
-
event.clientX, event.clientY
相对于视口的左上角
-
event.pageX,event.pageY
相对于页面的左上角
-
event.offsetX,event.offsetY
相对于事件元素的左上角
$(document).mousemove(function(e){
$("span").text(e.pageX + ", " + e.pageY);
});
-
mouseout([[data],fn])
当鼠标指针从元素上移开时,发生 mouseout 事件
$("p").mouseout(function(){
$("p").css("background-color","#E9E9E4");
});
-
mouseover([[data],fn])
当鼠标指针位于元素上方时,会发生 mouseover 事件
$("p").mouseover(function(){
$("p").css("background-color","yellow");
});
-
mouseup([[data],fn])
当在元素上放松鼠标按钮时,会发生 mouseup 事件
$("button").mouseup(function(){
$("p").slideToggle();
});
-
resize([[data],fn])
当调整浏览器窗口的大小时,发生 resize 事件
$(window).resize(function(){
alert("Stop it!");
});
-
scroll([[data],fn])
当用户滚动指定的元素时,会发生 scroll 事件
$(window).scroll( function() {
alert("Stop it!");
});
-
select([[data],fn])
当 textarea 或文本类型的 input 元素中的文本被选择时,会发生 select 事件
$("input").select();
-
submit([[data],fn])
当提交表单时,会发生 submit 事件
$("form:first").submit();
$("form").submit( function () {
return false;
} );
-
unload([[data],fn])
在当用户离开页面时,会发生 unload 事件
点击某个离开页面的链接
在地址栏中键入了新的 URL
使用前进或后退按钮
关闭浏览器
重新加载页面
$(window).unload( function () { alert("Bye now!"); } );