為什么remove和detach事件一樣,誰能解釋一下
<html>
<head>
??? <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
??? <style type="text/css">
??? p{
??????? border: 1px solid red;
??? }
??? </style>
??? <script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script>
</head>
<body>
??? <h3>給頁面2個p元素節點綁定點擊事件,點擊后彈出自己本身的節點內容</h3>
??? <p>元素p1,同時綁定點擊事件</p>
??? <p><span>666</span>元素p2,同時綁定點擊事件</p>
??? <h3>通過點擊2個按鈕后觀察方法處理的區別</h3>
??? <button>點擊通過remove處理元素p1</button>
??? <button id="btn">復制</button>
??? <button>點擊通過detach處理元素p2</button>
</body>
<script type="text/javascript">
??? var p;
??? //給頁面上2個p元素都綁定時間
??? $('p').click(function(e) {
??????? alert(e.target.innerHTML)
??? })
?? ?
??? $("button:first").click(function() {
???????? p = $("p:first").remove();
??? });
??? $("button:last").click(function() {
????????? p = $("p:first").detach();
??? });
??? $("#btn").click(function(){??? ?
??????? alert(p);
??????? $("body").append(p);
??? })
?? ?
</script>
</script>
</html>
2016-04-28
你的測試有問題,remove()刪除元素之后文本內容會被保存,只是jQuery綁定的事件和數據會被銷毀,而decath()會保存jQuery綁定的事件和數據
2016-07-07
課件講的有錯誤,remove()不會把匹配的元素從 jQuery 對象中刪除,因而可以在將來再使用這些匹配的元素,其他的比如綁定的事件、附加的數據等都會被移除。所以你用remove()刪除后,依然能使用append()再把它找回,但先前綁定在p1上的點擊事件已經沒有了,而使用detach()就不會讓綁定在上面的事件消失
2016-04-28
detach會保留jQuery的事件和數據啊