[英]How to get the original html string in innerHTML?
問題:當我嘗試使用關於jsx代碼的Highlight.js時,請消失html標簽。
原始資料:
<pre>
<code class="javascript">
const { Link } = ReactRouter;
App.QnAChild = React.createClass({
mixins: [Mixins.Accounts, Mixins.Utils],
render() {
let name = "";
if (this.props.user && this.props.user.name) {
name = this.props.user.name;
}
return (
<tr>
<td>
{this.props.index + 1}
</td>
<td>
<Link to={`/qna/${ this.props._id }`}
className="title-link"> {this.props.title}
</Link>
</td>
<td>
<Link to="">
{name}
</Link>
</td>
<td>
{this.momentKoreanDate(this.props.createdAt, 5)}
</td>
</tr>
)
}
});
</code>
</pre>
渲染源之后:
const { Link } = ReactRouter;
App.QnAChild = React.createClass({
mixins: [Mixins.Accounts, Mixins.Utils],
render() {
let name = "";
if (this.props.user && this.props.user.name) {
name = this.props.user.name;
}
return (
{this.props.index + 1}
{this.props.title}
{name}
{this.momentKoreanDate(this.props.createdAt, 5)}
)
}
});
$('pre code')[0] .innerHTML:
"const { Link } = ReactRouter; App.QnAChild = React.createClass({ mixins: [Mixins.Accounts, Mixins.Utils], render() { let name = ""; if (this.props.user && this.props.user.name) { name = this.props.user.name; } return ( {this.props.index + 1} <link to="{`/qna/${" this.props._id="" }`}="" classname="title-link"> {this.props.title} <link to=""> {name} {this.momentKoreanDate(this.props.createdAt, 5)} ) } }); "
為什么消失innerHTML中的html標簽? 如何在innerHTML中獲取原始的html字符串?
我希望在render之后獲得資源:
const { Link } = ReactRouter;
App.QnAChild = React.createClass({
mixins: [Mixins.Accounts, Mixins.Utils],
render() {
let name = "";
if (this.props.user && this.props.user.name) {
name = this.props.user.name;
}
return (
<tr>
<td>
{this.props.index + 1}
</td>
<td>
<Link to={`/qna/${ this.props._id }`}
className="title-link"> {this.props.title}
</Link>
</td>
<td>
<Link to="">
{name}
</Link>
</td>
<td>
{this.momentKoreanDate(this.props.createdAt, 5)}
</td>
</tr>
)
}
});
謝謝您的耐心:)
祝你今天愉快!
"<" = < (lower then)
">" = > (greater then)
<pre> <code class="javascript"> const { Link } = ReactRouter; App.QnAChild = React.createClass({ mixins: [Mixins.Accounts, Mixins.Utils], render() { let name = ""; if (this.props.user && this.props.user.name) { name = this.props.user.name; } return ( <tr> <td> {this.props.index + 1} </td> <td> <Link to={`/qna/${ this.props._id }`} className="title-link"> {this.props.title} </Link> </td> <td> <Link to=""> {name} </Link> </td> <td> {this.momentKoreanDate(this.props.createdAt, 5)} </td> </tr> ) } }); </code> </pre>
您應該將代碼存儲在變量中:
var myCode = "<html>";
var processed = yourProcessor(myCode);
$('pre code').innerHTML = processed;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.