IE 7 中 div 内容丢失换行符
我试图在源代码中的换行符上拆分 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用
"\n"
而不是"\r\n"
进行拆分。要同时执行这两项操作,请考虑按模式
"\r?\n"
进行拆分。Try splitting on
"\n"
instead of"\r\n"
.To do both, consider splitting on the pattern
"\r?\n"
.换行符是空白,通常不被保留。 它们与空间的含义相同。
Newlines are whitespace, and are not generally preserved. They mean the same thing as a space does.
IE 确实会丢失元素内容中的换行符,除了:
pre
(和plaintext
,本世纪没有使用过)textarea
中,它在其中还添加了虚假的 '\r',它们不计入其自己的字符计数机制但是,无论如何,这都行不通:
与 Java 不同,JavaScript 的 String.split 采用简单的分隔符字符串,而不是正则表达式模式。
IE does lose newlines in element content, except:
pre
(andplaintext
, not that that's ever used this century)textarea
, where it also adds spurious ‘\r’s that don't count towards its own character-counting mechanismsHowever, regardless of that, this won't work:
JavaScript's String.split, in contrast to Java's, takes a simple delimiter string, not a regex pattern.