str=str.replace(/&/g,"&");
str=str.replace(/</g,"&lt;");
str=str.replace(/>/g,"&gt;");

解决方案 »

  1.   

    用下面函数处理一下strfunction HTMLEncode(str){
      str=str.replace(/&/g,"&amp;");
      str=str.replace(/</g,"&lt;");
      str=str.replace(/>/g,"&gt;");
      str=str.replace(/\n/g,"<br>");
      str=str.replace(/\"/g,"&quot;");
      return str;
    }
      

  2.   

    function CheckStr(str)
    str = trim(str)
    str = replace(str, ">", "&gt;")
    str = replace(str, "<", "&lt;")
    str = Replace(str, CHR(32), " ")
    str = Replace(str, CHR(9), " ")
    str = Replace(str, " ", "&nbsp;")
    str = Replace(str, CHR(34), "&quot;")
    str = Replace(str, CHR(39), "&#39;")
    str = Replace(str, vbCr, "")
    str = Replace(str, vbLf, "<BR>")
    str = Replace(str, vbCrLf, "<BR>")
    CheckStr = str
    end function
      

  3.   

    换种思路:
    用<xmp></xmp>按文本方式显示内容
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
    <title> New Document </title>
    <meta name="Generator" content="EditPlus">
    <meta name="Author" content="Seagle.K">
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    </head>
    <body>
    <script language='javascript'>
    function preview(str){
    xxWin=window.open('','newwindow','height=450,width=550,top=0,left=0,toolbar=no,menubar=no,scrollbars=yes, resizable=yes,location=no, status=no');
    xxWin.document.open("text/txt");
    xxWin.document.write(str);
    xxWin.document.close() ;
    }var a = "<body><b><font color=red>\nhello world</font></b><table border=1><tr><td>1</td><td>2</td></tr><tr><td>3</td><td>4</td></tr></table></body>";a = "<xmp>" + a + "</xmp>";
    preview(a);
    </script>
    </body>
    </html>