简体   繁体   English

如何使用JavaScript显示段落文字,包括 <p> 标签?

[英]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 &middot; Second'};
}

function MyComponent() {
  return <div dangerouslySetInnerHTML={createMarkup()} />;
}

But I will recomend using libraries like DomPurify or SanitizeHtmlReact 但我建议使用DomPurifySanitizeHtmlReact之类的

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 ={
 '<' : '&lt; ',
 '>' : '&gt'
 }`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript:将文本更改为段落标签 - Javascript : changing text into paragraph tags 在Rails中使用JavaScript检测或创建段落文本中的标签? - Detect or create tags in paragraph text with javascript in Rails? 使用JavaScript解析文本:神秘生成的段落标签 - Parsing text with javascript: mysteriously generated paragraph tags Javascript:如何获取p标签内的文本字符串数组 - Javascript: How to get array of strings of text within p tags 带有html标签的javascript子字符串文本 - Substring text with javascript including html tags 如何使用 JavaScript 将文本字符串截断为以省略号结尾的多个字符以用于段落标签数组 - How do I truncate a string of text to a number of characters ending with ellipsis for an array of paragraph tags using JavaScript JavaScript / jQuery-在切片文本内容中保留锚标记 - JavaScript / jQuery - Keep anchor tags in sliced paragraph text content 从段落中删除锚标记,但使用 Javascript 保留文本 - Remove anchor tags from a paragraph but keep the text using Javascript 如何成功显示和隐藏文本段落? - How to successfully show and hide a paragraph of text? 如何显示选择框 <p> 我在javascript中做p.text(“ test”)吗? - How do i show a selectbox when it is in <p> and i do p.text(“test”) in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM