2013-12-11 27 views
-4

如果存在,我需要刪除容器中的特定元素。下面的代碼提醒告訴我該元素存在,但無論出於何種原因,都不會刪除,並且不會提供任何錯誤消息。JQuery語法 - remove()將無法在元素上工作

var second_page_item = $('DIV#discount_0 > .element .first_static .second_page .isotope-item'); 
if($(second_page_item).length == 0) { 
    alert('it here'); 
    $(second_page_item).remove(); //WHY DOESNT IT REMOVE? 
} 
+5

如果長度等於零,你要刪除什麼? –

+1

奇怪的問題... –

+0

如何顯示警報? – giammin

回答

1

second_page_item已經是一個jQuery對象,所以沒有理由再次運行jQuery。另外,0的length意味着它不存在。這是您的代碼版本更合乎邏輯的方法。

var second_page_item = $('DIV#discount_0 > .element .first_static .second_page .isotope-item'); 
if (second_page_item.length > 0) { 
    second_page_item.length.remove(); 
} 

但是,你可以簡單地做。

$('DIV#discount_0 > .element .first_static .second_page .isotope-item').remove(); 
3

所以,如果它....

if($(second_page_item).length == 0) { 

....沒有長度,並且不存在,將其刪除

$(second_page_item).remove(); 

是非常合情合理的,但有什麼都不能刪除?