移除事件unbind()

unbind([type],[data])
1、没有参数,则删除所有的绑定事件
2、提供事件类型作为参数,则删除该类型的绑定事件
3、把绑定时传递的处理函数作为参数,则删除该函数 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Panel</title>
<style type="text/css">
*{margin:0;padding:0;}	
body { font-size: 13px; line-height: 130%; padding: 60px; }
p {width:200px;background:#888;color:white;height:16px;}
</style>
<script src="../../scripts/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
/*unbind([type],[data])
1、没有参数,则删除所有的绑定事件
2、提供事件类型作为参数,则删除该类型的绑定事件
3、把绑定时传递的处理函数作为参数,则删除该函数	  
*/
$(function() {
	$('#btn').bind("click", myFun1 = function() {
		$('#test').append("<p>我的绑定函数1</p>");
	}).bind("click", myFun2 = function() {
		$('#test').append("<p>我的绑定函数2</p>");
	}).bind("click", myFun3 = function() {
		$('#test').append("<p>我的绑定函数3</p>");
	});

//	//1、没有参数,则删除所有的绑定事件
//	$('#delTwo').click(function() {
//		$('#btn').unbind();
//	});
//	//2、提供事件类型作为参数,则删除该类型的绑定事件
//	$('#delTwo').click(function() {
//		$('#btn').unbind("click");
//	});
	//3、把绑定时传递的处理函数作为参数,则删除该函数	  
	$('#delTwo').click(function() {
		$('#btn').unbind("click", myFun2);
	});

})
</script>
</head>
<body>
<button id="btn">点击我</button>
<div id="test"></div>
<button id="delTwo">删除第二个事件</button>
</body>
</html>

参考《锋利的jQuery(第2版)》

猜你喜欢

转载自blog.csdn.net/LzzMandy/article/details/82592296
今日推荐