jquery如果存在2,如何删除div的实例?

jquery如果存在2,如何删除div的实例?,jquery,filter,find,Jquery,Filter,Find,这个问题听起来有点让人困惑,但我的回答是: <div id="type2"> <img class="profile-img" src="75.jpg"> </div> 我试过: if (item.lenght == 2) { $('#type2').find('.profile-img:first').remove(); } 但它似乎不起作用 谢谢 编辑: 谢谢大家。这是shifty给我的答案: if ($('#type2').find('.pr

这个问题听起来有点让人困惑,但我的回答是:

<div id="type2">
<img class="profile-img" src="75.jpg">
</div>
我试过:

    if (item.lenght == 2) {
$('#type2').find('.profile-img:first').remove();
}
但它似乎不起作用 谢谢

编辑:

谢谢大家。这是shifty给我的答案:

if ($('#type2').find('.profile-img').size() >= 2) {
$('#type2').find('.profile-img:first').remove();
}
我确实拼错了
长度
,但仍然不想工作:)

这应该可以工作:

$('.profile-img','#type2').eq(0).remove();

这应该起作用:

$('.profile-img','#type2').eq(0).remove();

代码
$('#type2').find('.profile img:first').remove()
肯定会删除
#type2
选择器中带有类
profile img
的第一个元素,因此在其他地方会出现问题

是否只是因为您在
item.lenght
中拼错了
length
属性?

代码
$('#type2')。查找('.profile img:first')。删除()
肯定会删除
#type2
选择器中带有类
profile img
的第一个元素,因此在其他地方会出现问题

是不是因为您在
item.lenght
中拼错了
length
属性

if ($('#type2').find('.profile-img').size() >= 2) {
  $('#type2').find('.profile-img:first').remove();
}
这应该行得通

if ($('#type2').find('.profile-img').size() >= 2) {
  $('#type2').find('.profile-img:first').remove();
}

你的方法应该行得通。我猜问题在于“lenght”的拼写错误:


当然,假设
已定义且长度正好为2。

您的方式应该可以正常工作。我猜问题在于“lenght”的拼写错误:


当然,假设
已定义且长度正好为2。

是的,我也这么认为。他的选择器很好,正如你指出的,他拼错了“长度”,是的,我也这么认为。他的选择器很好,正如您所指出的,他拼写错误了“length”(长度)。等式(0)在代码的某些部分对我有效,而('nth-child(1)')在代码的其他部分对我有效。等式(0)在代码的某些部分对我有效,而('nth-child(1)')在代码的其他部分对我有效。
$('#type2 .profile-img:first').remove();
$('.profile-img:first','#type2').remove();
if ($('#type2').find('.profile-img').size() >= 2) {
  $('#type2').find('.profile-img:first').remove();
}
if (item.length == 2) {
    $('#type2').find('.profile-img:first').remove();
}