在 <textarea> 中可以有多行 HTML5 占位符文本吗? [英] Can you have multiline HTML5 placeholder text in a <textarea>?
问题描述
当您使用 HTML5 的占位符属性将注意力集中在文本字段中时,我的文本字段中的幽灵文本会消失:
I have ghost text in textfields that disappear when you focus on them using HTML5's placeholder attribute:
<input type="text" name="email" placeholder="Enter email"/>
我想使用相同的机制在 textarea 中包含多行占位符文本,可能是这样的:
I want to use that same mechanism to have multiline placeholder text in a textarea, maybe something like this:
<textarea name="story" placeholder="Enter story
next line
more"></textarea>
但是那些
出现在文本中并且不会导致换行......有没有办法拥有多行占位符?
But those
s show up in the text and don't cause newlines... Is there a way to have a multiline placeholder?
更新:我让它起作用的唯一方法是利用 jQuery Watermark 插件,它接受占位符文本中的 HTML:
UPDATE: The only way I got this to work was utilizing the jQuery Watermark plugin, which accepts HTML in the placeholder text:
$('.textarea_class').watermark('Enter story<br/> * newline', {fallback: false});
推荐答案
For s 规范 特别概述了占位符属性中的回车+换行符必须被浏览器渲染为换行符.
For <textarea>
s the spec specifically outlines that carriage returns + line breaks in the placeholder attribute MUST be rendered as linebreaks by the browser.
当元素的值是空字符串并且控件没有聚焦时(例如,通过在空白的未聚焦控件中显示它),用户代理应该向用户呈现这个提示.提示中的所有 U+000D CARRIAGE RETURN U+000A LINE FEED 字符对 (CRLF),以及提示中的所有其他 U+000D CARRIAGE RETURN (CR) 和 U+000A LINE FEED (LF) 字符,都必须处理作为呈现提示时的换行符.
User agents should present this hint to the user when the element's value is the empty string and the control is not focused (e.g. by displaying it inside a blank unfocused control). All U+000D CARRIAGE RETURN U+000A LINE FEED character pairs (CRLF) in the hint, as well as all other U+000D CARRIAGE RETURN (CR) and U+000A LINE FEED (LF) characters in the hint, must be treated as line breaks when rendering the hint.
也反映在 MDN 上:https://developer.mozilla.org/en-US/docs/Web/HTML/Element/textarea#attr-placeholder
Also reflected on MDN: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/textarea#attr-placeholder
FWIW,当我尝试使用 Chrome 63.0.3239.132 时,它确实像它所说的那样工作.
FWIW, when I try on Chrome 63.0.3239.132, it does indeed work as it says it should.
这篇关于在 <textarea> 中可以有多行 HTML5 占位符文本吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!