简体   繁体   English

javascript:在textarea中显示值

[英]javascript:Display value in textarea

I`m trying to display default value in textarea,on click it should clear existing data and onblur it should display default text.Following is my code-> 我正在尝试在textarea中显示默认值,单击它应该清除现有数据并onblur它应该显示默认文本。以下是我的代码->

<textarea class="TextArea" id="appfield" name="appfield" style="height:44px;" rows="2" onfocus="deletetext(this)" onblur="filltext(this)" value="defaulttext"></textarea>

value is not displaying in textarea.what`s wrong???We have similar case in this website in Title textbox.I have to implement the same in my case. 值未显示在textarea中。是什么错误?我们在该网站的“标题”文本框中有类似的情况。在我的情况下,我必须实现相同的情况。 Please suggest an answer. 请提出答案。

the correct way is 正确的方法是

<textarea ...>defaulttext</textarea>

but you should set up a working fiddle (functions included) to reproduce the issue. 但是您应该设置一个有效的提琴(包括功能)来重现该问题。

If you have an option and want to use HTML5 you can do it by using placeholder attribute. 如果您有一个选择并且想要使用HTML5,则可以通过使用placeholder属性来实现。

<textarea 
    class="TextArea" 
    id="appfield" 
    name="appfield" 
    style="height:44px; 
    rows="2" 
    placeholder="default text"
    value=""></textarea>

Other option is 其他选择是

<textarea 
    onfocus="if(this.value==this.defaultValue)this.value='';"
    onblur="if(this.value=='')this.value=this.defaultValue;"
    class="TextArea"
    id="appfield" 
    name="appfield" 
    style="height:44px; 
    rows="2">default text</textarea>

You can use this 你可以用这个

 <textarea  id="username"  name="username" 
onfocus="if(this.value=='somevalue') { this.value='';this.style.color='#333333';}" 

onblur="if(this.value=='') {this.value='somevalue'; this.style.color='#B2B2B2';}">
somevalue
</textarea>

Instead of using JavaScript you can use placeholder. 代替使用JavaScript,可以使用占位符。 jsfiddle . jsfiddle but placeholder not supported by older browsers. 但是旧版浏览器不支持占位符。 you can use jquery libratry . 您可以使用jquery libratry

<textarea class="FullTextArea" id="applyfieldscontrol" name="applyfieldscontrol" style="height:44px"  placeholder="defaulttext"></textarea>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM