javascript - 如何在文本编辑器中包含 html 标签?[附上片段]
问题描述
我正在使用react-draft- wysiwyg和draftjs-to-html制作文本编辑器。
而且我已经将动态html注入到编辑器中,比如,
index.js:
export default function App() {
const dynamicData = `<div class="heading"> This text needs to display along with the <span> html tags </span> surrounded </div>`;
const handleInputChange = (e) => {
console.log("event ", e.target.value);
};
return (
<>
<ContentEditor
name="dynamicContent"
value={dynamicData}
onChange={(event) => handleInputChange(event)}
/>
</>
);
}
当前工作场景:
在这里,事情运行得很好,我能够得到纯文本,例如,
This text needs to display along with the html tags surrounded
在编辑器里面。
注意:(我的意思是说只显示文本而不显示 div、span 等 html 标签。
预期的:
我需要绑定整个 HTML,就像它在编辑器中一样,
<div class="heading"> This text needs to display along with the <span> html tags </span> surrounded </div>
工作片段:
所以我的要求是需要在编辑器中显示文本,
<div class="heading"> This text needs to display along with the <span> html tags </span> surrounded </div>
而不是
This text needs to display along with the html tags surrounded
解决方案
尝试这个:
export default function App() {
function getConvertedText(text) {
console.log(text);
text = text.replace(/</g, "<").replace(/>/g, ">");
return text;
}
const dynamicData = '<div class="heading"> This text needs to display along with the ' + getConvertedText("<span> html tags </span>") + ' surrounded </div>';
const handleInputChange = (e) => {
console.log("event ", e.target.value);
};
return ( <
>
<
ContentEditor name = "dynamicContent"
value = {
dynamicData
}
onChange = {
(event) => handleInputChange(event)
}
/> <
/>
);
}
推荐阅读
- cordova - Ionic Cordova 资源错误:证书链中的自签名证书
- selenium - 有没有办法将 webdriverio 测试编译为标准硒文件
- dart - Flutter - 如何制作嵌套列表视图?
- javascript - 在基本的 vanilla js 页面中使用 systemjs
- c# - 如何在属性类型不固定的情况下反序列化 json - 可以是空字符串或对象,请建议。如何处理这种情况?
- php - 如何检查一个数组的值是否等于另一个数组的值并显示在选择下拉列表中选择的值
- mongodb - MongoDB 中使用的数据结构(B 树等)
- python - 如何将获得的数据包存储到 pkt.show() 命令的变量中?
- c# - 在 android 中启动后应用程序崩溃。获取 Java.Lang.ClassNotFoundException
- api - 调用带有扁平化或不扁平化主体的 HTTP POST API?