下面的代碼里,【去重】的一行是什么意思啊,無法理解
<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,默認給綁定一個點擊事件</p>
? ? <p>P元素2,默認給綁定一個點擊事件</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é)點還是保存在內存中
? ? ? ? //數(shù)據(jù)與事件都不會丟失
? ? ? ? p = $("p").detach()
? ? });
? ? $("#bt2").click(function() {
? ? ? ? //把p元素在添加到頁面中
? ? ? ? //事件還是存在
? ? ? ? $("body").append(p);
? ? });
? ? </script>
</body>
</html>
2016-11-08
判斷:如果P的長度為空就返回,不執(zhí)行下面的detach()代碼了。?if (!$("p").length)是if ($("p").length == null)的逼格寫法?