【问题标题】:How to get the original html string in innerHTML?如何获取innerHTML中的原始html字符串?
【发布时间】:2015-10-09 17:59:07
【问题描述】:

问题:当我尝试使用highlight.js关于jsx代码时,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 &amp;&amp; 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字符串?

我希望在渲染后来源:

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>
        )
    }
});     

感谢您的耐心:)

祝你有美好的一天!

【问题讨论】:

  • 应该是实体

标签: javascript jquery html innerhtml highlight.js


【解决方案1】:

"&lt;" = &amp;lt; (lower then)

"&gt;" = &amp;gt; (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 (
          &lt;tr&gt;
            &lt;td&gt;
              {this.props.index + 1}
            &lt;/td&gt;
            &lt;td&gt;
              &lt;Link to={`/qna/${ this.props._id }`}
                    className="title-link"> {this.props.title}
              &lt;/Link&gt;
            &lt;/td&gt;
            &lt;td&gt;
              &lt;Link to=""&gt;
                {name}
              &lt;/Link&gt;
            &lt;/td&gt;
            &lt;td&gt;
              {this.momentKoreanDate(this.props.createdAt, 5)}
            &lt;/td&gt;
          &lt;/tr&gt;
        )
      }
    });        
    </code>
</pre>

您应该将代码存储在一个变量中:

var myCode = "<html>";
var processed = yourProcessor(myCode);
$('pre code').innerHTML = processed;

【讨论】:

  • 谢谢!!我试图替换 source.. innerHTML.replace(/,"<"); innerHTML.replace(/>/gi,">");所以,我想在innerHTML中获取原始代码...... :)
  • 我发现消失到innerHTML中的表格标签(tr,td)...其他标签(div,span)存在于innerHTML中...我不明白..
  • @ProgrammingPearls 抱歉不明白你的意思,你能更新你的问题,或者提供一个例子吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-26
  • 2014-12-02
  • 2019-08-17
  • 1970-01-01
  • 2018-08-14
  • 2021-12-28
相关资源
最近更新 更多