实例代码“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
  $("p.test1").on("click",function(){
9
    $(this).css("background-color","pink");
10
  });
11
  $("#btn1").click(function(){
12
    $("p.test1").off();
13
  });
14
  $("p.test2").live("click",function(){
15
    $(this).css("background-color","yellow");
16
  });
17
  $("#btn2").click(function(){
18
    $("p.test2").die();
19
  });
20
});
21
</script>
22
</head>
23
<body>
24
25
<h4 style="color:green;">This example demonstrates how to achieve the same effect using off() and die().</h4>
26
27
<p class="test1">Click this paragraph to change its background color.</p>
28
<p class="test1">Click the button below and then click on this paragraph (the click event is removed).</p>
29
30
<button id="btn1">Remove the click event with off()</button>
31
32
<p class="test2">Click this paragraph to change its background color.</p>
33
<p class="test2">Click the button below and then click on this paragraph (the click event is removed).</p>
34
35
<button id="btn2">Remove the click event with die()</button>
36
37
</body>
38
</html>