Important Note ( As suggested by Jon Brave in the comments ) :
Placeholder Attribute does not set the value of a textarea. Rather "The placeholder attribute represents a short hint (a word or short phrase) intended to aid the user with data entry when the control has no value" [and it disappears as soon as user clicks into the textarea]. It will never act as "the default value" for the control. If you want that, you must put the desired text inside the Here is the actual default value, as per other answers here
If you want to bring information from a database into a textarea tag for editing:
The input tag not to display data that occupy several lines: rows no work, tag input is one line.
placeholder='' inserts your text, but it is greyed out (in a tool-tip style format) and the moment the field is clicked, your text is replaced by an empty text field.
value='' is not a <textarea> attribute, and only works for <input> tags, ie, <input type='text'>, etc. I don't know why the creators of HTML5 decided not to incorporate that, but that's the way it is for now.
The best method for inserting text into <textarea> elements has been outlined correctly here as: <textarea> Desired text to be inserted into the field upon page load </textarea> When the user clicks the field, they can edit the
text and it remains in the field (unlike placeholder='').
Note: If you insert text between the <textarea> and </textarea> tags, you cannot use placeholder='' as it will be overwritten by your inserted text.
发布评论
评论(10)
这是我的 jsFiddle 示例。这工作正常:
Here is my jsFiddle example. this works fine:
您可以使用 占位符 属性,它不会添加默认值,但可能正是您正在寻找的内容:
在此处查看 - http://jsfiddle.net/8DzCE/949/
重要说明(根据 Jon Brave 在评论中的建议):
Placeholder 属性不会设置文本区域的值。相反,“占位符属性表示一个简短的提示(单词或短语),旨在在控件没有值时帮助用户输入数据”[一旦用户单击文本区域,它就会消失]。它永远不会充当控件的“默认值”。如果您想要这样做,则必须将所需的文本放入此处是实际的默认值,按照此处的其他答案
You can use placeholder Attribute, which doesn't add a default value but might be what you are looking out for :
Check it out here - http://jsfiddle.net/8DzCE/949/
Important Note ( As suggested by Jon Brave in the comments ) :
Placeholder Attribute does not set the value of a textarea. Rather "The placeholder attribute represents a short hint (a word or short phrase) intended to aid the user with data entry when the control has no value" [and it disappears as soon as user clicks into the textarea]. It will never act as "the default value" for the control. If you want that, you must put the desired text inside the Here is the actual default value, as per other answers here
如果您想将数据库中的信息放入文本区域标签中进行编辑:
输入标签不显示占用多行的数据:rows不起作用,标签输入是一行。
textarea 标签没有值,但可以与车把配合使用
If you want to bring information from a database into a textarea tag for editing:
The input tag not to display data that occupy several lines: rows no work, tag input is one line.
The textarea tag has no value, but work fine with handlebars
以防万一,如果您在项目中使用 Angular.js(就像我一样),并且为您的
...将不起作用!
您需要将默认值设置为文本区域的
ng-model
在各自的控制器中或使用ng-init
。示例 1(使用
ng-init
):示例 2(不使用
ng-init
):Just in case if you are using Angular.js in your project (as I am) and have a
ng-model
set for your<textarea>
, setting the default just inside like:...will not work!
You need to set the default value to the textarea's
ng-model
in the respective controller or useng-init
.Example 1 (using
ng-init
):Example 2 (without using
ng-init
):一些注释和说明:
placeholder=''
插入您的文本,但它呈灰色(以工具提示样式格式),并且单击该字段时,您的文本将被替换通过空文本字段。value=''
不是将文本插入
当用户单击该字段时,他们可以编辑
文本,并且它保留在字段中(与
placeholder=''
不同)。标记之间插入文本,则不能使用
placeholder=''
将被您插入的文本覆盖。A few notes and clarifications:
placeholder=''
inserts your text, but it is greyed out (in a tool-tip style format) and the moment the field is clicked, your text is replaced by an empty text field.value=''
is not a<textarea>
attribute, and only works for<input>
tags, ie,<input type='text'>
, etc. I don't know why the creators of HTML5 decided not to incorporate that, but that's the way it is for now.The best method for inserting text into
<textarea>
elements has been outlined correctly here as:<textarea> Desired text to be inserted into the field upon page load </textarea>
When the user clicks the field, they can edit thetext and it remains in the field (unlike
placeholder=''
).<textarea>
and</textarea>
tags, you cannot useplaceholder=''
as it will be overwritten by your inserted text.占位符无法设置文本区域的默认值。 如果您将其用于数据库连接,则可以使用
This is 标签。如果您使用 php 之外的其他语言,您可以使用不同的语法。对于 php :
例如:
required 命令可以最大限度地减少使用 php 检查空字段所需的工作。
Placeholder cannot set the default value for text area. You can use
This is the tag if you are using it for database connection. You may use different syntax if you are using other languages than php.For php :
e.g.:
required command minimizes efforts needed to check empty fields using php.
另外,这对我来说非常有效:
在本例中,$_POST['encode'] 来自此:
PHP 代码插入在 和 标记之间。
Also, this worked very well for me:
In this case, $_POST['encode'] came from this:
The PHP code was inserted between the and tags.
您可以使用 this.innerHTML。
当文本区域获得焦点时,它基本上使文本区域的innerHTML成为空字符串。
You can use this.innerHTML.
When text area is focused, it basically makes the innerHTML of the textarea an empty string.
请注意,如果您在文本区域渲染后对其进行了更改;您将获得更新后的值而不是初始化值。
正如您所看到的,textarea 的值将与关注 textarea 的开始和结束标记之间的文本不同。
Please note that if you made changes to textarea, after it had rendered; You will get the updated value instead of the initialized value.
As you can see the value of textarea will be different than the text in between the opening and closing tag of concern textarea.
您还可以添加“value”属性并将其设置为如下所示:
You can also add the "value" attribute and set that so something like so: