简体   繁体   English

使用数据库中的html标签打印文本-Javascript

[英]Print text with html tags from database - Javascript

I am using CKEditor in my React App to save user content into the Firebase database. 我在我的React App中使用CKEditor将用户内容保存到Firebase数据库中。 Content is saved as I want it (eg. with html tags "<p>contentheheheh</p>\\n" ) but the problem occurs when I want to fetch the data. 内容按我想要的方式保存(例如,使用html标签"<p>contentheheheh</p>\\n" ),但是当我要获取数据时会出现问题。

As a result of fetch, let's say into a of I get text with html tags, not 'converted' into bold or italic for example. 由于抓取的结果,让我们说说我收到的带有html标签的文本,例如,未“转换”为粗体或斜体。

When I get into Chrome console and inspect element I see something like this: 当我进入Chrome控制台并检查元素时,会看到以下内容:

<td>&lt;p&gt;I am&amp;nbsp;&lt;strong&gt;testing&amp;nbsp;&lt;/strong&gt;&lt;em&gt;text&lt;/em&gt;&lt;/p&gt;

The thing is I worked with 'raw' db inputs before in Laravel or Symphony, but I don't know how to do it in javascript. 问题是我之前在Laravel或Symphony中使用过“原始”数据库输入,但是我不知道如何在javascript中进行操作。 I tried to look for it in google and stack but to be honest I failed because I don't even know what exactly is a question, since 'fetch raw data from db in javascript' didn't worked exactly as I imagined. 我试图在google和堆栈中查找它,但老实说我失败了,因为我什至不知道到底是什么问题,因为“从javascript中从db获取原始数据”并没有按照我的想象完全起作用。

Thanks for any advice in advance. 感谢您提前提出任何建议。

PS I know that in Jquery there was something like this: PS我知道在Jquery中有这样的事情:

var text = '&lt;p&gt;name&lt;/p&gt;&lt;p&gt;&lt;span style="font-size:xx-small;"&gt;ajde&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;em&gt;da&lt;/em&gt;&lt;/p&gt;';
var decoded = $('<textarea/>').html(text).text();
alert(decoded);

But I don't know how to write this in pure javascript (or ES6). 但是我不知道如何用纯JavaScript(或ES6)编写此代码。

You can do it with RegExp replacement: 您可以使用RegExp替换来做到这一点:

var text = '&lt;p&gt;I am&amp;nbsp;&lt;strong&gt;testing&amp;nbsp;&lt;/strong&gt;&lt;em&gt;text&lt;/em&gt;&lt;/p&gt;';

var decoded = text.replace(/"&amp;/g, "&").replace(/&gt;/g, ">").replace(/&lt;/g, "<").replace(/&quot;/g, "\""); 

Probably (without code sample i can't really know) the cause of what is happening doesn't come from fetch but from rendering. 可能(没有我不能真正知道的代码示例),发生情况的原因不是来自获取,而是来自渲染。

By default react escapes html tags when rendering a component. 默认情况下,渲染组件时react会转义html标签。

To avoid escaping and set html of a component use dangerouslysetinnerhtml ( https://reactjs.org/docs/dom-elements.html#dangerouslysetinnerhtml ). 为避免转义和设置组件的html,请使用dragonallysetinnerhtml( https://reactjs.org/docs/dom-elements.html#dangerouslysetinnerhtml )。

Okey so I spent at this problem whole night BEFORE I asked the question, and just after I did that, I managed to resolve the problem. 奥基(Okey),所以我在问这个问题之前整晚都在这个问题上度过,然后,我就设法解决了这个问题。

  1. I installed 'he' library from npm package (npm install he) 我从npm软件包安装了“他”库(npm安装他)
  2. I installed 'html-react-parser' (npm install html-react-parser). 我安装了“ html-react-parser”(npm安装html-react-parser)。

First one is used to decode html from database. 第一个用于从数据库解码html。 Second one is to write this html into jsx elements. 第二个是将此HTML写入jsx元素。

Example: 例:

let text = he.decode(props.tdData[item]);
return (<td>{Parser(text)}</td>);

This two libraries do it for me and it works just as I wanted. 这两个库为我做到了,它可以按我的意愿工作。

Thanks for any input you made, case is closed :) 感谢您的任何输入,案例已关闭:)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM