为什么删除最后一个孩子不起作用?
我试图删除div旧图像并使用最后一个孩子(jQuery)添加一个新图像,但它不起作用我得到一个新图像附加旧图像旁边。 这是jQuery代码
$(function() { $('.iconWrapper span').click(function(e){ var kleur=$(this).attr('class'); switch(kleur){ case 'color1': $("#div1 img:last-child").remove(); alert($("#div1").html()); $('#div1').append(''); break; case 'color2': $("#div1 img:last-child").remove(); $('#div1').append(''); break; case 'color3': $("#div1 img:last-child").remove(); $('#div1').append(''); break; } $('#hiddenimg').val(kleur); e.preventDefault(); });
HTML代码::
你可以尝试jQuery:last selector: link
因为你需要这样的东西:
$("#div1 img:last").remove();
或更好的版本
$("#div1 img").filter(":last").remove();
我现在不知道要测试,我希望这是你的工作解决方案:)
(抱歉我的英文:“>)
你需要:last
而不是:last-child