CDATA块在React

时间:2018-04-02 15:15:00

标签: javascript html reactjs cdata

我试图在ReactJS中将<![CDATA[ ]]块中的一些html传递给dangerouslySetInnerHTML,这个内容呈现为注释掉的。例如。我在DOM中获得了<!--[CDATA[Where is my text?]]-->而不是[CDATA[Where is my text?]]

&#13;
&#13;
const HTML_OK = '<pre>My text looks good</pre>'
const HTML_CDATA = '<pre><![CDATA[Where is my text?]]></pre>'

class Hello extends React.Component {
  render() {
    return <div dangerouslySetInnerHTML={{ __html: this.props.html }} />;
  }
}

ReactDOM.render(
	<div>
    <Hello html={HTML_OK} />
    <Hello html={HTML_CDATA} />
  </div>,
  document.getElementById('root')
);
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

<div id="root">
    <!-- This element's contents will be replaced with your component. -->
</div>
&#13;
&#13;
&#13;

如何在我的React应用程序中显示<![CDATA[ ]]

0 个答案:

没有答案