事件
一、常见事件
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
#e02{
border: 1px solid #000000;
height: 200px;
width: 200px;
}
</style>
<script type="text/javascript" src="../js/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#e01").blur(function(){
$("#textMsg").html("文本框失去焦点:blur");
}).focus(function(){
$("#textMsg").html("文本框获得焦点:focus");
}).keydown(function(){
$("#textMsg").append("键盘按下:keydown");
}).keypress(function(event){
$("#textMsg").append("键盘按:keypress");
}).keyup(function(){
$("#textMsg").append("键盘弹起:keyup");
}).select(function(event){
//支持谷歌
var sub = $(this).val().substring(event.target.selectionStart,event.target.selectionEnd);
$("#textMsg").html("文本内容被选中:select , " + sub);
});
var i = 0;
$("#e02").mouseover(function(){
$("#divMsg").html("鼠标移上:mouseover");
}).mousemove(function(){
$("#divMsg").html("鼠标移动:mousemove , " + i++ );
}).mouseout(function(){
$("#divMsg").html("鼠标移出:mouseout");
}).mousedown(function(){
$("#divMsg").html("鼠标按下:mousedown");
}).mouseup(function(){
$("#divMsg").html("鼠标弹起:mouseup");
});
$("#e03").click(function(){
$("#buttonMsg").html("单击:click");
}).dblclick(function(){
$("#buttonMsg").html("双击:dblclick");
});
});
</script>
</head>
<body>
<input id="e01" type="text" /><span id="textMsg"></span> <br/>
<hr/>
<div id="e02" ></div><span id="divMsg"></span> <br/>
<hr/>
<input id="e03" type="button" value="可以点击"/><span id="buttonMsg"></span> <br/>
</body>
</html>
l jQuery 提供额外的事件,用于完善javascript缺失的
l focusin 和 focusout
focusin获得焦点。js focus。
focusin事件跟focus事件区别在于,他可以在父元素上检测子元素获取焦点的情况。
focusout失去焦点。js blur。
focusout事件跟blur事件区别在于,他可以在父元素上检测子元素失去焦点的情况。
<script type="text/javascript">
$(document).ready(function(){
//js focus 父元素【不能检测】到子元素获得焦点
/*
$("#outerDiv").focus(function(){
alert("outer");
});
*/
// jquery focusin 父元素【可以检测】到子元素获得焦点
$("#outerDiv").focusin(function(){
alert("outer");
});
});
</script>
</head>
<body>
<div id="outerDiv" style="border:1px solid #f00;width:200px;height:200px">
<div id="innerDiv" style="border:1px solid #00f;width:100px;height:100px"></div>
</div>
<br/>
<span id="showSpan"></span>
</body>
l mouseenter 和 mouseleave
mouseenter鼠标移入。jsmouseover
与 mouseover 事件不同,只有在鼠标指针穿过被选元素时,才会触发 mouseenter 事件。如果鼠标指针穿过任何子元素,同样会触发 mouseover 事件。
mouseleave鼠标移出。jsmouseout
与 mouseout 事件不同,只有在鼠标指针离开被选元素时,才会触发 mouseleave 事件。如果鼠标指针离开任何子元素,同样会触发 mouseout 事件。
<script type="text/javascript">
$(document).ready(function(){
var i= 0;
//js mouseover 当鼠标在父元素和子元素 之间穿越时,【触发】父元素的事件。
/*
$("#outerDiv").mouseover(function(){
$("#showSpan").html(i++);
});
*/
//jquery mouseenter 当鼠标在父元素和子元素 之间穿越时,【不触发】父元素的事件。
$("#outerDiv").mouseenter(function(){
$("#showSpan").html(i++);
});
});
</script>
</head>
<body>
<div id="outerDiv" style="border:1px solid #f00;width:200px;height:200px">
<div id="innerDiv" style="border:1px solid #00f;width:100px;height:100px"></div>
</div>
<br/>
<span id="showSpan"></span>
</body>
二、页面加载
l 方式1:标准api
$(document).ready(function(){
});
等效
jQuery(document).ready( fn );
l 方式2:简化版
$(function(){
});
等效
jQuery(function(){
});
<script type="text/javascript">
// jquery 页面加载 可以使用多次
$(function(){
alert("aaaa");
});
$(function(){
alert("bbbb");
});
// js 只能执行一个,后面的覆盖前面的
window.onload = function(){
alert("1111");
};
window.onload = function(){
alert("2222");
};
</script>
三、事件绑定
1、处理
bind(type ,fn) 给当前对象绑定一个事件。例如:A.bind("click", fn ); 类型:A.click( fn);
unbind(type ) 解绑bind绑定事件
one(type ,fn ) 给当前对象绑定一次事件。
-----------------------------------------
on(events , fn) 提供绑定事件处理程序所需的所有功能。完成3个方法功能.bind(), .delegate(), 和 .live().
off(events) 解绑
trigger(type) 在每一个匹配的元素上触发某类事件。例如:A.trigger("submit") ,类似A.submit();
triggerHandler(type) 在每一个匹配的元素上触发某类事件。但不会执行浏览器默认动作,也不会产生事件冒泡。
2、委派
live(type , fn) 绑定事件,之后动态添加同样的成员,也具有相同的事件。
die(type) 解绑事件
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="../js/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(function(){
// <input id="h01" type="button" value="1只能点击一次,之后失效" />
$("#h01").one("click",function(){
alert("你只能看到我一次");
});
// <input id="h02" type="button" value="2可以点击多次" />
//jQuery 事件别名,格式:事件.别名
$("#h02").bind("click.a",function(){
alert("每次都是你111");
});
$("#h02").bind("click.b",function(){
alert("怎么还是你222");
});
// <input id="h03" type="button" value="3解绑2" />
$("#h03").click(function(){
$("#h02").unbind("click.b");
});
// <input type="button" value="4 添加一个按钮,样式为myClass,且拥有相同的事件" class="myClass" />
$(".myClass").live("click",function(){
$("body").append("<input type='button' class='myClass' />");
});
// <input id="h05" type="button" value="5 解绑4"/>
$("#h05").click(function(){
$(".myClass").die("click");
});
});
</script>
</head>
<body>
<input id="h01" type="button" value="1只能点击一次,之后失效" />
<input id="h02" type="button" value="2可以点击多次" />
<input id="h03" type="button" value="3解绑2" />
<input type="button" value="4 添加一个按钮,样式为myClass,且拥有相同的事件" class="myClass" />
<input id="h05" type="button" value="5 解绑4"/>
</body>
</html>
3、切换
hover(over , out)
简化版,鼠标移入和移出 ,A.mouseover( fn ).mouseout( fn) 简化 A.hover( fn, fn )
toggle( fn , fn , fn ,...) 执行click事件,每点击一次执行一个fn
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
#e02{
border: 1px solid #000000;
height: 200px;
width: 200px;
}
</style>
<script type="text/javascript" src="../js/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(function(){
$("#e01").toggle(function(){
alert("报数:1");
}, function(){
alert("报数:2");
});
$("#e02").hover(function(){
$("#divMsg").html("over");
} , function(){
$("#divMsg").html("out");
});
});
</script>
</head>
<body>
<input id="e01" type="button" value="点我呀 " /><span id="textMsg"></span> <br/>
<hr/>
<div id="e02" ></div><span id="divMsg"></span> <br/>
</body>
</html>