請問代碼中的“去重”是什么意思?
????<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é)點還是保存在內存中 ????????//數據與事件都不會丟失 ????????p?=?$("p").detach() ????});
請問代碼中,if (!$("p").length) return; //去重是怎么分析的呢?什么意思呢?為什么通過length來判斷去重呢?
2016-05-19
?$("#bt1").click(function()?{
????????if?(!$("p").length){
?????return;
????}
? ? ? ?p?=?$("p").detach()
????});
!0是true,直接return,函數結束。!2是false return不執(zhí)行,執(zhí)行detach?
2016-05-16
避免重復操作,如果p標簽已經被你刪除了,你再點不是沒意義了嗎,$("p").length此時為零,前面加!變成布爾型true,于是就執(zhí)行后面的return,不會往下執(zhí)行detach()了. ? length是代表長度么,$("p").length代表有幾個p元素