我想为我的html <textarea>设置一个默认值。我从材料中读到,要添加默认值,你必须做一些事情,如<textarea>这是默认文本</textarea>。我试过了,但没用。正确的做法是什么?


当前回答

如果你想把数据库中的信息放到文本区域标签中进行编辑: 输入标记不显示占用数行的数据:行不工作,标记输入的是一行。

<!--input class="article-input" id="article-input" type="text" rows="5" value="{{article}}" /-->

textarea标签没有值,但与句柄一起工作很好

<textarea class="article-input" id="article-input" type="text" rows="9" >{{article}}</textarea> 

其他回答

占位符不能为文本区域设置默认值。你可以使用

<textarea rows="10" cols="55" name="description"> /*Enter default value here to display content</textarea>

如果您将它用于数据库连接,则它是标记。如果使用php以外的其他语言,则可以使用不同的语法。对于php:

例如:

<textarea rows="10" cols="55" name="description" required><?php echo $description; ?></textarea>

Required命令最大限度地减少了使用php检查空字段所需的工作。

如果你想把数据库中的信息放到文本区域标签中进行编辑: 输入标记不显示占用数行的数据:行不工作,标记输入的是一行。

<!--input class="article-input" id="article-input" type="text" rows="5" value="{{article}}" /-->

textarea标签没有值,但与句柄一起工作很好

<textarea class="article-input" id="article-input" type="text" rows="9" >{{article}}</textarea> 

你可以使用this.innerHTML。

<textarea name="message" rows =" 10" cols =" 100" onfocus="this。innerHTML= " ">在这里输入您的消息…textarea > < /

当文本区域被聚焦时,它基本上会使文本区域的innerHTML成为空字符串。

你也可以添加"value"属性并设置如下:


<textarea value="your value"> </textarea>

几点注意事项和澄清:

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.