為什么remove和detach事件一樣,誰(shuí)能解釋一下
<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>給頁(yè)面2個(gè)p元素節(jié)點(diǎn)綁定點(diǎn)擊事件,點(diǎn)擊后彈出自己本身的節(jié)點(diǎn)內(nèi)容</h3>
??? <p>元素p1,同時(shí)綁定點(diǎn)擊事件</p>
??? <p><span>666</span>元素p2,同時(shí)綁定點(diǎn)擊事件</p>
??? <h3>通過(guò)點(diǎn)擊2個(gè)按鈕后觀察方法處理的區(qū)別</h3>
??? <button>點(diǎn)擊通過(guò)remove處理元素p1</button>
??? <button id="btn">復(fù)制</button>
??? <button>點(diǎn)擊通過(guò)detach處理元素p2</button>
</body>
<script type="text/javascript">
??? var p;
??? //給頁(yè)面上2個(gè)p元素都綁定時(shí)間
??? $('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
你的測(cè)試有問(wèn)題,remove()刪除元素之后文本內(nèi)容會(huì)被保存,只是jQuery綁定的事件和數(shù)據(jù)會(huì)被銷(xiāo)毀,而decath()會(huì)保存jQuery綁定的事件和數(shù)據(jù)
2016-07-07
課件講的有錯(cuò)誤,remove()不會(huì)把匹配的元素從 jQuery 對(duì)象中刪除,因而可以在將來(lái)再使用這些匹配的元素,其他的比如綁定的事件、附加的數(shù)據(jù)等都會(huì)被移除。所以你用remove()刪除后,依然能使用append()再把它找回,但先前綁定在p1上的點(diǎn)擊事件已經(jīng)沒(méi)有了,而使用detach()就不會(huì)讓綁定在上面的事件消失
2016-04-28
detach會(huì)保留jQuery的事件和數(shù)據(jù)啊