Javascript JQuery语法-remove()赢得';不要在元素上工作

Javascript JQuery语法-remove()赢得';不要在元素上工作,javascript,jquery,Javascript,Jquery,我需要删除容器中的特定元素(如果存在)。下面的代码提醒我该元素存在,但无论出于何种原因,它都不会删除,也不会提供任何错误消息 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).remo

我需要删除容器中的特定元素(如果存在)。下面的代码提醒我该元素存在,但无论出于何种原因,它都不会删除,也不会提供任何错误消息

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?
}
所以如果它

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?
}
if($(second_page_item).length == 0) {
。。。。没有长度,并且不存在,请删除它

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?
}
$(second_page_item).remove();

这很有道理,但是没有什么可以删除的?

第二个页面项目已经是jQuery对象,因此没有理由再次对其运行jQuery。另外,长度为0意味着它不存在。这是一种更符合逻辑的代码版本方法

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?
}
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();
}
然而,你可以简单地做到

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?
}
$('DIV#discount_0 > .element .first_static .second_page .isotope-item').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?
}
if($(second_page_item).length > 0) {
    alert('it here');
    $(second_page_item).remove();
}

如果长度等于零,您希望删除什么?如何显示该警报?您是否关注代码?看看这个again@giammin-如果选择器没有长度,即元素不存在,则会显示警报。我刚刚意识到,无论如何,警报都会显示,因此我认为我检查元素是否存在的方式是错误的