Javascript 无法删除img';带jQuery的s属性

Javascript 无法删除img';带jQuery的s属性,javascript,jquery,html,Javascript,Jquery,Html,我有一个HTML字符串,我想在发送到服务器之前编辑它。html字符串包含一个或多个img元素。在这些图像元素中,我想删除src属性,它是base64字符串 不幸的是,下面的代码似乎删除了整个图像元素。有人能看出我做错了什么吗 post.content是html字符串。我想找到这个字符串中的图像元素,然后为每个图像更改其src属性 // Remove the base 64 src const $content = $(post.content); const $img = $content.fi

我有一个HTML字符串,我想在发送到服务器之前编辑它。html字符串包含一个或多个img元素。在这些图像元素中,我想删除
src
属性,它是base64字符串

不幸的是,下面的代码似乎删除了整个图像元素。有人能看出我做错了什么吗

post.content是html字符串。我想找到这个字符串中的图像元素,然后为每个图像更改其
src
属性

// Remove the base 64 src
const $content = $(post.content);
const $img = $content.find('img');
$img.each(function(i) {
    $(this).attr('src', 'dummy');
});
post.content = $content.html();
console.log(post.content);
当我使用上述jQuery时post.content日志的结果

<br>

当我不使用上面的jquery、普通html字符串和带有base64 src的img时,post.content的结果

编辑1:我做了一个
$content
的console.log,下面是
$img
的console.log


因为您的
帖子内容包含多个节点。因此,当jquery解析它时,它将返回多个
节点的数组

然后最后一个
post.content=$content.html()将只包含第一个节点的内容

尝试将整个post.content包装在
中,如下所示:


const$content=$(“”).html(post.content);


查看此处的完整代码:

Do
console.log($content.html())
在设置
$content
后立即执行。我已将其添加到原始帖子中。哦,对不起,我做错了,2秒钟看起来它成功地更改为“dummy”,因为它正在进行(不成功)调用以检索它。如果你能将代码归结为一个(你可以这样做)。结果也是一样的。你是一个英雄。我终于成功了。非常感谢你,伙计。