jquery的remove()方法會將方法和數(shù)據(jù)移除,那么這個數(shù)據(jù)是指什么?
<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元素節(jié)點綁定點擊事件,點擊后彈出自己本身的節(jié)點內(nèi)容</h3>
? ? <p id="p1">元素p1,同時綁定點擊事件</p>
? ? <p>元素p2,同時綁定點擊事件</p>
? ? <h3>通過點擊2個按鈕后觀察方法處理的區(qū)別</h3>
? ? <button>點擊通過remove處理元素p1</button>
? ? <button>點擊通過detach處理元素p2</button>
</body>
<script type="text/javascript">
? ? $("p").on("click",function(e){
? ? ? ? alert(e.target.innerHTML);
? ? });
? ? $("button:first").on("click",function(){
? ? ? ? var p = $("p:first").remove();
? ? ? ? alert(p.attr("id"));
? ? ? ? p.css("color","red").text("remove后事件也消失了");
? ? ? ? $("body").append(p);
? ? });
? ? $("button:eq(1)").on("click",function(){
? ? ? ? var p = $("p:eq(1)").detach();
? ? ? ? $("body").append(p);
? ? });
</script>
</script>
</html>
我給p加了id,我remove后將該對象的idalert了一下,還是可以出來,我比較好奇所說的數(shù)據(jù)是指什么?
2017-01-18
這個問題很深,涉及到j(luò)s內(nèi)存處理機(jī)制。