实例代码“Ctrl+/”提示“F11/ESC”全屏 返回 格式化 恢复 运行
x
 
1
<!DOCTYPE html>
2
<html>
3
<head>
4
<script src="https://libs.baidu.com/jquery/1.10.2/jquery.min.js">
5
</script>
6
<script>
7
$(document).ready(function(){
8
  $("body").on("click","p",function(){
9
    $(this).css("background-color","pink");
10
  });
11
  $("#btn1").click(function(){
12
    $("body").off("click","p");
13
  });
14
  $("#btn2").click(function(){
15
    $("body").undelegate("p","click");
16
  });
17
});
18
</script>
19
</head>
20
<body>
21
22
<h4 style="color:green;">This example demonstrates how to achieve the same effect using off() and undelegate().</h4>
23
24
<p>Click this paragraph to change its background color.</p>
25
<p>Click the button below and then click on this paragraph (the click event is removed).</p>
26
27
<button id="btn1">Remove the click event with off()</button>
28
29
<button id="btn2">Remove the click event with undelegate()</button>
30
31
</body>
32
</html>