[英]How can I replace newline characters using javascript in IE8?
I've searched Stackoverflow for hours and hours, and nobody's solution works in Internet Explorer 8. 我已经搜索了数小时的Stackoverflow,但没人能在Internet Explorer 8中找到解决方案。
I am provided with a plaintext document like this: 我收到了像这样的纯文本文档:
This is a legal agreement ("Agreement") between you and ...
License
Subject to you continued and ongoing compliance with the terms and conditions set ...
Restrictions
Except as otherwise explicitly provided in this Agreement, ...
Ownership
Except for this license granted to you, as between you and ...
Disclaimer of Warranties
Use at your own risk. ...
And I need to replace the newline characters (linebreaks, carriage returns, whatever you want to call them) with double linebreaks ( <br/><br/>
) to make the text look more normal. 而且我需要用双换行符(
<br/><br/>
)替换换行符(换行符,回车符,无论您想称呼它们什么),以使文本看起来更普通。
The nl2br function from jQuery convert line breaks to br (nl2br equivalent) works fine in most browsers. jQuery的nl2br函数将换行符转换为br(等效于nl2br),在大多数浏览器中都能正常工作。 However, a client of mine uses IE8.
但是,我的客户使用IE8。
Go ahead and try the nl2br function using IE8 (or a modern Internet Explorer set to IE8 mode); 继续尝试使用IE8(或设置为IE8模式的现代Internet Explorer)使用nl2br函数; it doesn't work.
它不起作用。
Does anyone know why it doesn't work in IE8? 有谁知道为什么它在IE8中不起作用? And how to accomplish the goal?
以及如何实现目标?
PS I put some code here http://jsfiddle.net/L2Ufj/2/ and oddly enough it works in IE8 via jsfiddle, but if you copy it to somewhere else and run it for real, it won't work in IE8. PS我在这里http://jsfiddle.net/L2Ufj/2/放了一些代码 ,奇怪的是它可以通过jsfiddle在IE8中工作,但是如果将其复制到其他地方并真正运行它,它将无法在IE8中工作。
One way to get round this in IE8 is to convert the line-breaks into a 'token' that IE8 will recognise before it is rendered on the page. 在IE8中解决此问题的一种方法是将换行符转换为IE8 在呈现在页面上之前可以识别的“令牌”。 Then once it's rendered, in a success handler for example you can search for that token and replace with
<br>
or whatever you wish: 然后,一旦渲染完成,例如,在成功处理程序中,您可以搜索该令牌并替换为
<br>
或所需的任何内容:
eg 例如
Pre render (I've used < br > as my token but you can use anything) 渲染前(我已经使用<br>作为令牌,但是您可以使用任何东西)
textToEdit = textToEdit.replace(/\n/g, '<br>');
Post render (Search for your token and replace with <br>
or whatever you wish) 发布渲染(搜索令牌并替换为
<br>
或您想要的任何符号)
renderedTextWrapper.innerHTML = renderedTextWrapper.innerHTML.replace(/<br>/g, '<br>');
When you retrieve element's innerHTML, IE will convert the innerHTML to a "standard-format" (by collapsing multi-spaces into one, removing linebreak, etc...) before giving you the result. 当您检索元素的innerHTML时,IE会在将结果提供给您之前将innerHTML转换为“标准格式”(通过将多个空格折叠为一个,删除换行符等)。
Thus, you can not find any linebreak character in the innerHTML you get with IE. 因此,您无法在通过IE获得的innerHTML中找到任何换行符。 What a bad news.
真是个坏消息。
I think the most feasible & easy approach is to store your text inside <textarea>
tag instead of normal <div>
. 我认为最可行,最简单的方法是将文本存储在
<textarea>
标记内,而不是普通的<div>
。 IE will leave <textarea>
alone when you get it's value instead of innerHTML: 当您获得
<textarea>
的值而不是innerHTML时,IE将<textarea>
会它:
originalText=document.getElementById('EULA_content').value
Of course, when you get the newText, you should append it to another div element. 当然,当您获得newText时,应将其附加到另一个div元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.