【JQ】jq动态绑定事件.on()、解绑事件off()

时间:2023-03-09 16:03:52
【JQ】jq动态绑定事件.on()、解绑事件off()

#JQ 绑定与解绑事件的方法的历史演变

  1、 jquery1.4 及之前的版本,由.click() 或 .bind()方法绑定的事件,不能适用脚本创建的新元素即是说页面加载完成后,再动态创建的DOM元素并不能响应之前绑定的事件!解绑事件使用.unbind()方法;

  旧版本的处理方法是使用.live()方法来代替事件绑定.bind(),使得绑定的事件能适用脚本创建的新元素,从而实现事件的动态绑定;解绑事件使用.unlive()方法;

 <html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("p").live("click",function(){
$(this).slideToggle();
});
$("button").click(function(){
$("<p>This is a new paragraph.</p>").insertAfter("button");
});
});
</script>
</head>
<body>
<p>这是一个段落。</p>
<p>点击任意 p 元素会令其消失。包括本段落。</p>
<button>在本按钮后面插入新的 p 元素</button>
<p><b>注释:</b>通过使用 live() 方法而不是 bind() 方法,新的 p 元素同样会在点击时消失。</p>
</body>
</html>

  在线测试.live()方法地址:http://www.w3school.com.cn/tiy/t.asp?f=jquery_event_live_newelement

  2、但自jq1.7版本开始,官方已明文表示不再推荐使用使用.live()方法,官方解释为:live()调用过程首先将事件方法绑定到了Document,然后,查找Document里是否有匹配元素。 这个过程对于性能来说可能比较浪费。官方推荐将.live()改成Delegate()方法,适用脚本动态创建的新元素。解绑使用.undelegate()方法;

 <html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("div").delegate("p","click",function(){
$(this).slideToggle();
});
$("button").click(function(){
$("<p>这是一个新段落。</p>").insertAfter("button");
});
});
</script>
</head>
<body>
<div style="background-color:yellow">
<p>这是一个段落。</p>
<p>请点击任意一个 p 元素,它会消失。包括本段落。</p>
<button>在本按钮后面插入一个新的 p 元素</button>
</div>
<p><b>注释:</b>通过使用 delegate() 方法,而不是 live(),只有 div 元素中的 p 元素会受到影响。</p>
</body>
</html>

  在线测试.delegate()方法地址:http://www.w3school.com.cn/tiy/t.asp?f=jquery_event_delegate_newelement

  3、jq1.8开始,官方又再次申明:如果你开发最新版本的jQuery,完全可以使用on()方法来处理所有的事件绑定,避免过多的方法调用,因为其实在最新版本的jQuery类库中,所有以上旧版方法在后面其实都是调用on()方法。解绑事件使用.off()方法;

#.on()与.off()的具体使用方法

  1、.on()的使用

<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#div1").on("click","p",function(){
$(this).css("background-color","pink");
});
});
</script>
</head>
<body> <h4 style="color:green;">This example demonstrates how to achieve the same effect using on() and delegate().</h4> <div id="div1">
<p>Click to set background color using the <b>on() method</b>.</p>
</div> </body>
</html>

   添加多个事件处理程序方法:

 $(document).ready(function(){
$("p").on("mouseover mouseout",function(){
$("p").toggleClass("intro");
});
});

  2、.off()的使用

 <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
<script src="https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("body").on("click","p",function(){
$(this).css("background-color","pink");
});
$("#btn1").click(function(){
$("body").off("click","p");
}); });
</script>
</head>
<body> <h4 style="color:green;">该事件演示了如何使用 off() 和 undelegate() 方法来达到相同的效果。</h4> <p>点击段落修改背景颜色。</p>
<p>点击以下按钮再点击这个段落 ( click 事件被移除)。</p> <button id="btn1">使用 off() 方法移除 click 事件</button> </body>
</html>