delegate绑定与解除绑定

今天遇到了一个问题,就是在用delegate绑定时,绑定的触发函数执行两次,代码如下:


<!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=GB2312" />
<title>Untitled Document</title>
<script src="js/jquery-1.6.min.js" type="text/javascript"></script>
<script>
$(function () {				
	$("body").delegate("div","mouseover",function(){
  		console.log("mouseover");
	});
});
function test(){
	$("body").undelegate();
	$("body").delegate("div","mouseover",function(){
  		console.log("mouseover2");
	});
}
</script>

</head>

<body>
<div style="color:#0F9; width:100px; height:100x; margin:100px; background-color:#903" id="dd">dededede
1111111111
3333333333</div>
<input type="button" value="sure" onclick="test()" />
</body>
</html>
项目的要求,在同一页面无刷新加载地图,那么被监听div会被监听两次(上面的代码是模拟的)

后来在第二次监听前使用undelegate函数对div解除绑定即可达到效果。


你可能感兴趣的:(delegate绑定与解除绑定)