HTML textarea忽略第一行字符,为什么? [英] HTML textarea ignores 1st new line character, why?

查看:295
本文介绍了HTML textarea忽略第一行字符,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您能解释一下为什么吗?

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>&#13;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.

&#13; == carriage return

这篇关于HTML textarea忽略第一行字符,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆