Jquery 为什么两张图片都是预加的?

Jquery 为什么两张图片都是预加的?,jquery,prepend,Jquery,Prepend,我正在尝试在img包装div中预先添加一个图像,并将其包装在post title锚点的href中。也许这让人困惑。代码如下: $(".post").each(function() { if($(this).find(".post-excerpt > p:first-child").has("img").length){ if($(this).find(".post-title").has("a").length){ $(this

我正在尝试在img包装div中预先添加一个图像,并将其包装在post title锚点的href中。也许这让人困惑。代码如下:

     $(".post").each(function() {
    if($(this).find(".post-excerpt > p:first-child").has("img").length){

        if($(this).find(".post-title").has("a").length){

            $(this).find(".img-wrapper").prepend("<a href='"+$(this).find(".post-title a").attr("href")+"'></a>");

            $(this).find(".post-excerpt > p:first-child").has("img").find("img").prependTo($(this).find(".img-wrapper a"));

        }else{

            $(this).find(".post-excerpt > p:first-child").has("img").find("img").prependTo($(this).find(".img-wrapper"));
        }
    }
});

同样,我正在尝试仅将映像预处理到img包装器div。谢谢。

我已尝试在本地系统上复制您的代码,发现映像出现两次,原因是:

".img-wrapper a", instead you have to use it as ".img-wrapper > a:first-child" after prepending anchor.
在这里拉小提琴:


由于路径原因,图像未加载,但已正确设置前缀。

谢谢。这是有道理的。
".img-wrapper a", instead you have to use it as ".img-wrapper > a:first-child" after prepending anchor.