var pre = document.createElement("pre");
pre.innerHTML = id("content").value;//content是textarea标签的ID在FF,SAFARI,OPERA等浏览器都会保存换行,但在IE里就把换行替换成空格了,请问大虾有什么好的办法解决呢?

解决方案 »

  1.   

    IE7 和 FF3 下测试通过L@_@K
    <!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>
      <title> new document </title>
      <meta name="generator" content="editplus" />
      <meta name="author" content="" />
      <meta name="keywords" content="" />
      <meta name="description" content="" />
     </head> <body>
      <textarea id="content" rows="10" cols="40"></textarea>
      <input type="button" id="btnAppend" value="append pre" />
      <script type="text/javascript">
      <!--
    function $(sId) {
    return document.getElementById(sId);
    }$("content").value = "123\r   456";$("btnAppend").onclick = function() {
    var pre = document.createElement("pre"); if (window.navigator.appName == "Microsoft Internet Explorer")
    pre.innerText = $("content").value;
    else
    pre.innerHTML = $("content").value; document.body.appendChild(pre);
    };  //-->
      </script>
     </body>
    </html>
      

  2.   

    IE是够变态的
    <!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=utf-8" />
    <title>无标题文档</title>
    <style type="text/css">
    pre{
    border:1px solid red;
    }
    </style>
    </head>
    <body>
    <textarea id="txt1" rows="10" cols="50">1
    2
    3
    4
    5</textarea>
    <pre>
    1
    2
    3
    4
    5
    </pre>
    <script type="text/javascript">
    function $(sId){
    return document.getElementById(sId);
    }(function(){
    var sHtml=$("txt1").value;
    var oPre=document.createElement("pre");
    var oTxtNode=document.createTextNode(sHtml);

    sHtml=oTxtNode.nodeValue;
    oTxtNode.nodeValue=sHtml.replace(/\r\n/g,"\n");

    oPre.appendChild(oTxtNode);
    document.body.appendChild(oPre);
    })();
    </script>
    </body>
    </html>