[英]How to show paragraph text with JavaScript including <p> tags?
i`m new with JavaScript and reactjs . 我是JavaScript和reactjs的新手。 i have a text that contain some paragraph tags like 我的文字包含一些段落标签,例如
newline and ... from a textarea with CKEDITOR. 换行符和...来自CKEDITOR的文本区域。 how can i show saved text in a html tag? 如何在html标签中显示保存的文本? for example i want to show this text : 例如我想显示此文本:
myString=<p>Cumque et blanditiis magnam optio amet. Eum eos et et et sit autem optio. Asperiores fuga iure quis sequi voluptas laboriosam et.</p> <div className={'myclass'}>myString</div>
in a html tag? 在一个HTML标签?
为此,请危险地使用SetInnerHTML :
<div className={'myclass'} dangerouslySetInnerHTML={{__html: myString}}/>
The process of cleaning a string of its html tags or other malicious javascript code is called sanitization. 清除其html标签或其他恶意javascript代码的字符串的过程称为清理。
One method is to use reactjs's 一种方法是使用reactjs的
dangerouslySetInnerHTML 危险地设置内部HTML
dangerouslySetInnerHTML is React's replacement for using innerHTML in the browser DOM. angerouslySetInnerHTML是React在浏览器DOM中使用innerHTML的替代。 In general, setting HTML from code is risky because it's easy to inadvertently expose your users to a cross-site scripting (XSS) attack. 通常,从代码设置HTML是有风险的,因为很容易无意间使您的用户遭受跨站点脚本(XSS)攻击。 So, you can set HTML directly from React, but you have to type out dangerouslySetInnerHTML and pass an object with a __html key, to remind yourself that it's dangerous. 因此,您可以直接从React设置HTML,但是您必须输入angerousedSetInnerHTML并使用__html键传递对象,以提醒自己这很危险。 For example: 例如:
function createMarkup() {
return {__html: 'First · Second'};
}
function MyComponent() {
return <div dangerouslySetInnerHTML={createMarkup()} />;
}
But I will recomend using libraries like DomPurify or SanitizeHtmlReact 但我建议使用DomPurify或SanitizeHtmlReact之类的库
because they are more flexible and has many options. 因为它们更灵活,并且有很多选择。
Check their examples and demos for more info 查看他们的示例和演示以获取更多信息
For this, we can write a method to accept String and replace HTML special character to like 为此,我们可以编写一个方法来接受String并将HTML特殊字符替换为
var charMap ={
'<' : '< ',
'>' : '>'
}`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.