HTML textarea忽略第一行字符,为什么? [英] HTML textarea ignores 1st new line character, why?
问题描述
您能解释一下为什么吗?
Could you explain why this:
<script type="text/javascript">
document.write("<textarea cols='10' rows='10'>" + "\nhello\nbabe\n" + "</textarea>");
</script>
提供一个文本区域,底部有一个新行,但顶部没有新行?
renders a textarea with one new line at the bottom, but NO new line at the top?
经过测试的IE8,FF11,Safari 5.1,Chrome 24
Tested IE8, FF11, Safari 5.1, Chrome 24
这不是JS问题,即使您在页面中编写HTML也会得到相同的结果,即
And it's not a JS issue, even when you write HTML in page you get the same result, i.e.
<textarea cols='10' rows='10'>
hello
babe
</textarea>
第一个新行仍然缺失!!!
The 1st new line is still missing!!!
我需要在顶部添加另一行以显示一个新行:
I need to add another new line at the top in order to show one:
document.write("<textarea cols='10' rows='10'>" + "\n\nhello\nbabe\n" + "</textarea>");
推荐答案
在XHTML内编写时,请使用适当的实体.
When writing inside of XHTML use proper entities.
<textarea> hello</textarea>
如果文本节点以空格开头(空格,换行),则HTML解析器将忽略它.将新行编码为适当的HTML实体会强制解析器对其进行确认.
If a text node begins with white space (space, new line) it will be ignored by HTML parsers. Encoding the new line into a proper HTML entity forces the parser to acknowledge it.
== carriage return
这篇关于HTML textarea忽略第一行字符,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!