我有一个 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 时 post.content 的结果,一个普通的 html 字符串和带有 base64 src 的 img
编辑1:我做的的console.log $content
,下面是一个的console.log$img
以及
由于您post.content
包含多个节点。所以当jquery解析它时,它会返回一个由多个<p>
节点组成的数组。
那么最后一个post.content = $content.html();
将只包含第一个节点的内容。
尝试将整个 post.content 包裹在这样的 a 中<div>
:
const $content = $("<div>").html(post.content);
在此处查看完整代码:https : //jsfiddle.net/vzxq0ckf/1/
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句