实例 jQuery 显示detach() 和 remove() 方法之间的差异。

x
 
<!DOCTYPE html>
<html>
<head>
<script src="https://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("#btn1").click(function(){
    $("body").append($("#p1").detach());
  });
  $("#btn2").click(function(){
    $("body").append($("#p2").remove());
  });
  $("p").click(function(){
    $(this).animate({fontSize: "+=1px"})
  });
});
</script>
</head>
<body>
<p id="p1">Click this paragraph after it is moved - it keeps its click event.</p>
<p id="p2">Click this paragraph after it is moved - it does not keeps its click event.</p>
<button id="btn1">Detach and append p element</button>
<button id="btn2">Remove and append p element</button>
</body>
</html>
                    

输出结果