gpt4 book ai didi

javascript - 将 img 替换为其 data 属性值

转载 作者:行者123 更新时间:2023-12-03 00:58:22 25 4
gpt4 key购买 nike

var t = "";
$("div.postswrapper[data-value="+id2+"]").find("div.text.mainpost").children("p").each(function(p){
t+= $(this).text().trim()+"\n";
});

我现在有这个,它的工作是获取所有段落标签并将它们添加到字符串中。很简单。现在我面临的最困难的部分是:

段落中可能包含 0 到 n 个 img 标签。例如

<p>hello<img src='https.imgur/image.png' data-raw='[img]https.imgur/image.png[/img]'>world!</p>

每个标签中都有一个字符串,其中包含一些原始数据,我需要将这些原始数据替换为字符串。例如,上面的内容是

你好[img]https.imgur/image.png[/img]世界!

我已经尝试过

t+= $((this).find('img').replaceWith($(this).find('img').attr('data-raw')).text()).text().trim()+"\n";

但由于 (this).find 函数在当前上下文中不存在,它严重失败

如有任何帮助,我们将不胜感激。

最佳答案

使用find查找img标签,然后使用replaceWith函数

 var t = "";
$("div.postswrapper[data-value="+id2+"]").find("div.text.mainpost").children("p").each(f unction(p){
p.find('img').replaceWith(function() { return this.data-raw; });
t+= $(this).text().trim()+"\n";
});

关于javascript - 将 img 替换为其 data 属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52728942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com