關(guān)于detach方法
<html> <head> ????<meta?http-equiv="Content-type"?content="text/html;?charset=utf-8"?/> ????<script?src="http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></script> ????<style?type="text/css"> ????p?{ ????????color:?red; ????} ????</style> </head> <body> ????<p>P元素1,默認(rèn)給綁定一個(gè)點(diǎn)擊事件</p> ????<p>P元素2,默認(rèn)給綁定一個(gè)點(diǎn)擊事件</p> ????<button?id="bt1">點(diǎn)擊刪除?p?元素</button> ????<button?id="bt2">點(diǎn)擊移動(dòng)?p?元素</button> ????<script?type="text/javascript"> ????$('p').click(function(e)?{ ????????alert(e.target.innerHTML) ????}) ????var?p; ????$("#bt1").click(function()?{ ????????if?(!$("p").length)?return;?//去重 ????????//通過detach方法刪除元素 ????????//只是頁面不可見,但是這個(gè)節(jié)點(diǎn)還是保存在內(nèi)存中 ????????//數(shù)據(jù)與事件都不會(huì)丟失 ????????p?=?$("p").remove(); ????}); ????$("#bt2").click(function()?{ ????????//把p元素在添加到頁面中 ????????//事件還是存在 ????????$("body").append(p); ????}); ????</script> </body> </html>
這里我把detach改成了remove,怎么還是能夠append呢?
2017-03-16
p?=?$("p").remove(); ?p是一個(gè)對(duì)象,存儲(chǔ)的是兩個(gè)p節(jié)點(diǎn) 【<p>P元素1,默認(rèn)給綁定一個(gè)點(diǎn)擊事件</p>?<p>P元素2,默認(rèn)給綁定一個(gè)點(diǎn)擊事件</p>】,
remove刪除了兩個(gè)p節(jié)點(diǎn)及其綁定的事件,把刪除的兩個(gè)p節(jié)點(diǎn)存在了變量p里面,
detach刪除了兩個(gè)p節(jié)點(diǎn),但是其綁定的事件還在,一同存在了變量p里面,
bt2 點(diǎn)擊事件的時(shí)候又把變量p加到body上了