IE8与近代(IE9至IE11)及现代(Chrome和Firefox)浏览器,对于\r和\n的处理方式不同。

需求:对于textarea中输入的换行,要在页面中表现出来。
方案:输入内容在展示时,通过将\r和\n替换为标签,代码如下:

以上代码,在IE9-11、Chrome、Firefox中运行良好,但在IE8下无效。
调试后发现,原来IE8中通过innerHTML拿出的字符串,被移除了\r和\n。

IE8
IE9
chrome
firefox

参考资料:
Document.createElement()
https://developer.mozilla.org/zh-CN/docs/Web/API/Document/createElement
element.innerHTML
https://developer.mozilla.org/zh-CN/docs/Web/API/Element/innerHTML
Node.nodeValue
https://developer.mozilla.org/zh-CN/docs/Web/API/Node/nodeValue
Node.textContent
https://developer.mozilla.org/zh-CN/docs/Web/API/Node/textContent

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注