本帖最后由 webstandards 于 2014-01-10 00:27:44 编辑

解决方案 »

  1.   

    什么编程语言?php 用htmlspecialchars_decode($str);
    JS代码参考:http://phpjs.org/functions/htmlspecialchars_decode/function htmlspecialchars_decode (string, quote_style) {
      var optTemp = 0,
        i = 0,
        noquotes = false;
      if (typeof quote_style === 'undefined') {
        quote_style = 2;
      }
      string = string.toString().replace(/&lt;/g, '<').replace(/&gt;/g, '>');
      var OPTS = {
        'ENT_NOQUOTES': 0,
        'ENT_HTML_QUOTE_SINGLE': 1,
        'ENT_HTML_QUOTE_DOUBLE': 2,
        'ENT_COMPAT': 2,
        'ENT_QUOTES': 3,
        'ENT_IGNORE': 4
      };
      if (quote_style === 0) {
        noquotes = true;
      }
      if (typeof quote_style !== 'number') {
        quote_style = [].concat(quote_style);
        for (i = 0; i < quote_style.length; i++) {
          if (OPTS[quote_style[i]] === 0) {
            noquotes = true;
          } else if (OPTS[quote_style[i]]) {
            optTemp = optTemp | OPTS[quote_style[i]];
          }
        }
        quote_style = optTemp;
      }
      if (quote_style & OPTS.ENT_HTML_QUOTE_SINGLE) {
        string = string.replace(/&#0*39;/g, "'");
      }
      if (!noquotes) {
        string = string.replace(/&quot;/g, '"');
      }
      string = string.replace(/&amp;/g, '&');
      return string;
    }
      

  2.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    <script type="text/javascript">
    window.onload=function(){
    var itemContent="&lt;p&gt;8864的萨芬的萨芬55&lt;/p&gt;"+
    "&lt;p&gt;8864的萨芬的萨芬55&lt;/p&gt;"
    var k=document.createTextNode(itemContent);
    itemContenter.appendChild(k);
    }
    </script>
    </head><body>
    <div id="itemContenter"></div>
    </body>
    </html>
    建个textNode试试
      

  3.   

    <div class="itemContent" id="itemContenter">
    &lt;p&gt;
    8864的萨芬的萨芬55
    &lt;/p&gt;
    </div>
    <script>
        function decodehtml(s) {
            return s.replace(/&gt;/gi, '>').replace(/&lt;/gi, '<');
        }
        var d = document.getElementById('itemContenter');
        d.innerHTML = decodehtml(d.innerHTML);
    </script>
      

  4.   

    从数据库里读出来的字符串是已经被转义过了的,要在浏览器显示时再把它反转义。没进行反转义是下面这个效果。
    <div class="itemContent" id="itemContenter">
    &lt;p&gt;
    8864的萨芬的萨芬55
    &lt;/p&gt;
    </div>我用了上面的反转义函数过了一遍,失败。
    <script type="text/javascript">
    var itemContent="&lt;p&gt;8864的萨芬的萨芬55&lt;/p&gt;
    &lt;p&gt;8864的萨芬的萨芬55&lt;/p&gt;"
    itemContent=itemContent.replace(/&gt;/g,">").replace(/&lt;/g,"<");
    itemContenter.innerHTML=itemContent;
    </script>---------->函数写成这样就没问题了。
    <script type="text/javascript">
    itemContenter.innerText = strFromDB   <----从数据库得到的原始数据
    itemContenter.innerHTML=itemContenter.innerText;
    </script>
      

  5.   

    如果可以,最好還是從源頭找起,把數據從存入資料庫的時候或從資料庫取出數據的時候,就把換行改成&lt;br&gt;