IE 7 中 div 内容丢失换行符

发布于 2024-07-15 08:11:14 字数 432 浏览 4 评论 0原文

我试图在源代码中的换行符上拆分 div 的内部文本。 这适用于:

$('div').text().split("\r\n|\r|\n")

但在 IE 7 中这会失败,因为换行符显然被删除了。 jQuery 1.3.2 似乎没有问题,因为我也尝试了这两种方法:

$('div')[0].innerText.split("\r\n|\r|\n")

并且

$('div')[0].innerHTML.split("\r\n|\r|\n")

如果我将 div 更改为 pre,则上述任何操作都不会起作用。

看起来换行符是在源被解析到 DOM 中之后出现的。 :( 是这样吗?有没有办法用 javascript 来获取它们?

I'm trying to split the innerText of a div on the newlines in the source for it. This works fine with:

$('div').text().split("\r\n|\r|\n")

But this fails in IE 7, due to the newlines apparently being stripped out. jQuery 1.3.2 doesn't appear to be at fault since i also tried both:

$('div')[0].innerText.split("\r\n|\r|\n")

and

$('div')[0].innerHTML.split("\r\n|\r|\n")

Neither do any of the above work if i change the div to a pre.

It appears that the newlines are once the source is parsed into the DOM. :( Is this so? Is there no way to get at them with javascript?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

隐诗 2024-07-22 08:11:14

尝试使用 "\n" 而不是 "\r\n" 进行拆分。

要同时执行这两项操作,请考虑按模式 "\r?\n" 进行拆分。

Try splitting on "\n" instead of "\r\n".

To do both, consider splitting on the pattern "\r?\n".

余生共白头 2024-07-22 08:11:14

换行符是空白,通常不被保留。 它们与空间的含义相同。

Newlines are whitespace, and are not generally preserved. They mean the same thing as a space does.

挽手叙旧 2024-07-22 08:11:14

IE 确实会丢失元素内容中的换行符,除了:

  • 中的 pre(和 plaintext,本世纪没有使用过)
  • textarea 中,它在其中还添加了虚假的 '\r',它们不计入其自己的字符计数机制

但是,无论如何,这都行不通:

分割("\r\n|\r|\n")

与 Java 不同,JavaScript 的 String.split 采用简单的分隔符字符串,而不是正则表达式模式。

IE does lose newlines in element content, except:

  • in pre (and plaintext, not that that's ever used this century)
  • in textarea, where it also adds spurious ‘\r’s that don't count towards its own character-counting mechanisms

However, regardless of that, this won't work:

split("\r\n|\r|\n")

JavaScript's String.split, in contrast to Java's, takes a simple delimiter string, not a regex pattern.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文