一定要用append嗎?
重新顯示detach的元素一定要用append顯示嗎?試了prepend、after和before,發(fā)現(xiàn)都可以實現(xiàn),想知道原因。
<html> <head> <meta?http-equiv="Content-type"?content="text/html;?charset=utf-8"?/>???? <script?src="http://idcbgp.cn/static/lib/jquery/1.9.1/jquery.js"></script>???? <style?type="text/css">???? p?{????????color:?red;????}???? </style> </head> <body>???? <div?id="d1"?style="width:?100px;height:?50px;background:?#CCC;"></div>???? <p>P元素1,默認(rèn)給綁定一個點擊事件</p>???? <p>P元素2,默認(rèn)給綁定一個點擊事件</p>???? <button?id="bt1">點擊刪除?p?元素</button>???? <button?id="bt2">點擊移動?p?元素</button>???? <script?type="text/javascript">???? $('p').click(function(e)?{???????? alert(e.target.innerHTML)????})???? var?p;???? $("#bt1").click(function()?{???????? if?(!$("p").length)?return;?//去重???????? //通過detach方法刪除元素???????? //只是頁面不可見,但是這個節(jié)點還是保存在內(nèi)存中???????? //數(shù)據(jù)與事件都不會丟失???????? p?=?$("p").detach()????});???? $("#bt2").click(function()?{???????? //把p元素在添加到頁面中???????? //事件還是存在???????? //$("body").append(p);???????? $("#d1").after(p)????});???? </script> </body> </html>
2019-01-03
他只是舉例子 實際上都可以的