【问题标题】:how to decode html in div encoded at server side?如何在服务器端编码的 div 中解码 html?
【发布时间】:2016-04-17 03:32:49
【问题描述】:

我正在使用 asp.net 应用程序。我在服务器端在 JSON 的一个节点中生成 JSON 的地方有 html 格式的文本,所以我使用了 httputility.htmlencode(); 所以在客户端我得到了它

<h1><span style="color: #ff0000;"><strong><span style="text-decoration: underline;">added general commetns</span></strong></span></h1>

当我尝试在 Div 中渲染它时,它也在以上述方式渲染。而不是我想看到没有

这怎么可能?

【问题讨论】:

    标签: javascript jquery asp.net json


    【解决方案1】:

    重复的问题,但这是来自https://stackoverflow.com/a/1249234/5780021的答案

    function htmlDecode(input){
      var e = document.createElement('div');
      e.innerHTML = input;
      return e.childNodes[0].nodeValue;
    }
    
    htmlDecode("<h1><span style="color: #ff0000;"><strong><span style="text-decoration: underline;">added general commetns</span></strong></span></h1&
    

    结果是:

    <h1><span style="color: #ff0000;"><strong><span style="text-decoration: underline;">added general commetns</span></strong></span></h1>
    

    【讨论】:

    • 这在服务器端不起作用,因为没有document
    猜你喜欢
    • 2011-08-31
    • 1970-01-01
    • 1970-01-01
    • 2013-10-23
    • 1970-01-01
    • 1970-01-01
    • 2019-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多