Javascript html作出反应:如何替换所有';img&x27;在保留样式的同时使用React组件的元素?
这个问题与图书馆有关 假设我有以下html字符串:Javascript html作出反应:如何替换所有';img&x27;在保留样式的同时使用React组件的元素?,javascript,html,css,reactjs,Javascript,Html,Css,Reactjs,这个问题与图书馆有关 假设我有以下html字符串: ' “ 通常,我会将其解析如下: import {Parser} from 'html-to-react' const reactElement = Parser.parse(htmlString); 这将生成一个React元素,其中包含一个div和两个嵌套的img元素作为其子元素 如何改为解析此字符串,使每个img都替换为自定义图像组件,从而保留img的原始道具/属性(包括样式) e、 g.生成的react元素将具有以下子元素: 谢谢
'
“
通常,我会将其解析如下:
import {Parser} from 'html-to-react'
const reactElement = Parser.parse(htmlString);
这将生成一个React元素,其中包含一个div
和两个嵌套的img
元素作为其子元素
如何改为解析此字符串,使每个
img
都替换为自定义图像
组件,从而保留img
的原始道具/属性(包括样式)
e、 g.生成的react元素将具有以下子元素:
谢谢你的帮助!谢谢请尝试以下方法:
const { Parser } = require('html-to-react');
const htmlString = '<div>
<img src="test.png" style="width: 100px;"/>
<img src="test2.png" style="margin: 0px 4px;"/>
</div>';
const reactComponent = Parser.parseWithInstructions(
htmlString,() {
return true;
},[
{replaceChildren: true}
]
);
const{Parser}=require('html-to-react');
常量htmlString=
';
const reactComponent=Parser.parseWithInstructions(
htmlString,(){
返回true;
},[
{replaceChildren:true}
]
);
Hi@Aman,你能描述一下这个代码片段的功能吗?我看不出img
元素在哪里被Image
元素所取代。有可能我之前没有很好地沟通我的问题(我现在在OP中添加了一些额外的细节)