简体   繁体   English

IE 8浏览器不支持新行字符“\\ n”

[英]New line character “\n” not supported in IE 8 browser

I have use the below HTML code snippet to display text, but unable to do so in Internet Explorer 8 version. 我使用下面的HTML代码段来显示文本,但无法在Internet Explorer 8版本中执行此操作。

 <!DOCTYPE html>
     <html xmlns="http://www.w3.org/1999/xhtml">
     <head>
       <title></title>
          <script src="Scripts/jquery-1.10.2.js"></script>
          <script src="jquery-ui-1.8.20.js"></script>
     </head>
     <body>
       <div>
         <pre id="test">
         </pre>
      </div>

 <script type="text/javascript">
    $(document).ready(function () {
        $("#test").html("WELCOME\nHello World")
    });
  </script>
 </body>
</html>

This code snippet works fine as expected in IE's latest versions but “ \\n ” is not working in IE8 version . 此代码段在IE的最新版本中正常工作,但“ \\ n ”在IE8版本中不起作用。 How to resolve this? 怎么解决这个? Thanks in advance. 提前致谢。

Try more correct DOM manipulation: 尝试更正确的DOM操作:

document.getElementById("test")
    .appendChild(document.createTextNode("WELCOME\nhelloworld"));

It should work. 它应该工作。

Also , you can use outerHTML to force render in IE: 此外 ,您可以使用outerHTML在IE中强制渲染:

var el = document.getElementById("test")
if (el.tagName == "PRE" && "outerHTML" in el) { // check `outerHTML` exists (it's not in Firefox)
    var outer = el.outerHTML;
    var preservedAttrs = outer.substring(0, outer.indexOf('>') + 1);
    el.outerHTML = preservedAttrs + str + "</PRE>";
}
else {
    el.innerHTML = str;
}

Another way is to use nodeValue to preserve text from IE preprocessing: 另一种方法是使用nodeValue来保留IE预处理中的文本:

document.getElementById('test').firstChild.nodeValue = "WELCOME\nhelloworld";  

为确保与IE 8的兼容性,请尝试使用\\ r \\ n而不是仅使用\\ n

In all cases, you should replace the \\n with <br/> since you are submitting HTML, not text. 在任何情况下,你应该替换\\n<br/>因为你要提交HTML,而不是文字。 The \\n should have had issues in all browsers. \\n应该在所有浏览器中都有问题。

\\r\\n is the windows linebreak. \\r\\n是windows换行符。 \\n works for linux. \\n适用于Linux。

Having said that, \\r\\n will not work if you intend to use it to break text to another line in the rendered HTML page. 话虽如此,如果您打算使用它来将文本分解为呈现的HTML页面中的另一行, \\r\\n将不起作用。 For that you should use <br/> . 为此你应该使用<br/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM