【问题标题】:Convert decimal HTML entities to unicode characters from string将十进制 HTML 实体从字符串转换为 Unicode 字符
【发布时间】:2018-08-03 04:36:30
【问题描述】:

我有一个从评论系统中提取的 html 文件,我必须使用它,看起来像这样:

<div>Plz say my name&amp;#128516;&amp;#9996;&amp;#127995;</div>
<div>Dose Amelia like donuts &amp;#127849;&amp;#127849;</div>

显然,要注意的部分是所有表情符号。然后我将那个 HTML 文件拉入使用

var questionsfile = "comments.inc";
$.get(questionsfile, function(response) {
    $(".noquestions").replaceWith(response);
});

我使用.replace() 将其转换回&amp;#9996;,但现在它只是将其输出为纯文本。如何使response 输出包含✌?????

【问题讨论】:

    标签: javascript unicode html-entities


    【解决方案1】:

    您需要将它的text 替换为它的html,查看下面的演示

    var str = `<div>Plz say my name&amp;#128516;&amp;#9996;&amp;#127995;</div>
    <div>Dose Amelia like donuts &amp;#127849;&amp;#127849;</div>`;
    
    var $str = $( str );
    $str.find( "div" ).each( function(){ //iterate each div
        $(this).text( $(this).html() ); //replace text with html
    })
    
    $(".noquestions").replaceWith(  $str );
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="noquestions-wrapper">
      <div class="noquestions">
         assasd
      </div>
    </div>

    【讨论】:

    • 文本只是response 中的一个示例,所以我尝试了您的建议,例如$(response).find("div").each( function(){ //iterate each div $(this).text( $(this).html() ); //replace text with html }),但它仍然只是在浏览器中输出文本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-22
    • 1970-01-01
    • 2017-05-16
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    相关资源
    最近更新 更多