如何更换某些回车换行符后跟一个带有< br /> ;?的短划线?
如何使用javascript或jQuery替换网页中TD元素内的文本中的序列ASCII 13 ASCII 10 - (CR LF DASH或/ r / n-)? stackoverflow和其他地方也存在类似的问题,但列出的解决方案不适用于此特定方案。传入的HTML是由我的客户遗留软件动态生成的,不会被更改,但它引用了我可以更改的javascript文件。下面给出了页面的缩短版本。实际页面包含0到20行数据,其中一些包含多行。我的用户在Internet Explorer 8上,以下两行中的任何一行都不起作用。我试过更换回车只是替换换行。我从javascript和jQuery尝试过这个,没有任何明显的效果。当我从Internet Explorer 8保存页面并在十六进制编辑器中查看时,回车符和换行符存在于客户端中。问题的症结在于将文本中的/ n暴露给JavaScript。
How can I replace the sequence ASCII 13 ASCII 10 - (CR LF DASH or /r/n-) in the text inside a TD element in a web page using javascript or jQuery? There are similar quesitons about this in stackoverflow and elsewhere, but the solutions listed don't work on this particular scenario. The incoming HTML is generated dynamically by a piece of my customer's legacy software that is not going to be changed, but it references a javascript file that I can change. A shortened version of the page is given below. The actual page contains between zero and twenty rows of data, some of which contain multiple lines. My users are on Internet Explorer 8 and neither of the two lines below is working. I've tried just replacing the carriage return and just replacing the line feed. I've tried this from javascript and jQuery without any visible effect. When I save the page from Internet Explorer 8 and view it in a hex editor, the carriage return and line feed characters are present in the client. The crux of the problem is exposing the /n in the text to JavaScript.
我想执行此替换,因为我希望在显示的文本中显示两行文本在元素的页面中存在序列/ r / n-的任何时候输出。
I want to perform this replacement because I want the two lines of text to appear in the displayed output any time the sequence /r/n- exist in the page in a element.
请注意,我已经包含了两个版本的替换,一个jQuery和一个JavaScript 。也不是我想要它做的。
Note that I've included two versions of the replacement, one jQuery and one JavaScript. Neither does what I want it to do.
<html>
<head>
<title>Page Testing </title>
<script src="js/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('td').each(function () {
$this = $(this);
$this.html($this.html().replace(/\r\n\-/g, "<br/>-"));
this.innerHTML = (this.innerHTML.replace(/\r\n\-/g, "<br/>-"));
});
});
</script>
</head>
<body>
<table>
<tbody>
<tr>
<td>-First Content
-Second Line of Content</td>
<td>-How I want it to look<br/>-After the transformation</td>
</tr>
</tbody>
</table>
</body>
</html>
你的问题说你只关心将它们分开展示行,而不是专门更换\\ n。
Your question says you only care about DISPLAYING them on separate lines, not specifically about replacing the \r\n.
您可以考虑用css来解决这个问题。
You could consider css to solve this problem.
<style>
tr {
white-space:pre-line;
}
</style>