如何清除点击textarea?
给定一个<textarea>
有默认值,如下所示:如何清除点击textarea?
<textarea>Please describe why</textarea>
如何清除,当用户点击编辑的字段的默认值?
你的JavaScript:
function clearContents(element) {
element.value = '';
}
而且你的HTML:
<textarea onfocus="clearContents(this);">Please describe why</textarea>
我想你会希望这一点更加坚固,以便不擦用户输入时第二次聚焦。 Herearefiverelateddiscussions & articles。
而这里的(much better) idea是戴维·多沃德是指上述评论:
<label for="explanation">Please describe why</label>
<textarea name="explanation" id="explanation"></textarea>
<textarea onClick="javascript: this.value='';">Please describe why</textarea>
为什么减1,它符合海报的要求。 – user318747 2010-05-06 19:02:25
downvote不是我的,但是这个解决方案每次都会删除文本字段*用户点击它。 – 2010-05-06 19:59:24
尝试:
<input name="mytextbox" onfocus="if (this.value=='Please describe why') this.value = ''" type="text" value="Please Describe why">
这不是一个textarea,所以-1。这只会删除示例文本,所以+1。 – 2010-05-06 19:29:50
你是说像这样的文本字段?
<input type="text" onblur="if(this.value == '') this.value='SEARCH';" onfocus="if(this.value == 'SEARCH') this.value='';" size="15" value="SEARCH" name="xSearch" id="xSearch">
或者这对于textarea?
<textarea id="usermsg" rows="2" cols="70" onfocus="if(this.value == 'enter your text here') this.value='';" onblur="if(this.value == '') this.value='enter your text here';" >enter your text here</textarea>
最好的解决办法是什么我知道,到目前为止:
<textarea name="message" onblur="if (this.value == '') {this.value = 'text here';}" onfocus="if (this.value == 'text here') {this.value = '';}">text here</textarea>
为什么不能简单地用占位符属性?
<textarea placeholder="Please describe why"></textarea>
这会将您的填充文本置于灰色,当用户单击文本字段时将自动消失。此外,如果失去焦点并且文本区域为空白,它将重新出现。
这仅限于HTML5。 – harrymc 2012-07-04 10:29:24
<textarea name="post" id="post" onclick="if(this.value == 'Write Something here..............') this.value='';" onblur="if(this.value == '') this.value='Write Something here..............';" >Write Something here..............</textarea>
其最佳方式 – Heart 2012-05-05 16:18:19
这是你的JavaScript文件:
function yourFunction(){
document.getElementById('yourid').value = "";
};
这是HTML文件:
<textarea id="yourid" >
Your text inside the textarea
</textarea>
<button onClick="yourFunction();">
Your button Name
</button>
jQuery的属性VAL()应该做的工作。 你可以做$('#YourTextareaID').click(function(e) { e.target.value = ''; })
。
'val()'是一个jQuery属性,不是纯JS,你需要提一下。 – william44isme 2013-09-08 19:03:53
请尝试下面的链接到我找到的页面。这似乎是一个非常好的解决方案,如果用户输入数据并再次点击该框,则不会清除文本。
http://www.yourhtmlsource.com/forms/clearingdefaulttext.html
你可以试试这个代码,
<textarea name="textarea" cols="70" rows="2" class="searchBox" id="textarea" onfocus="if(this.value == 'Please describe why') this.value='';" onblur="if(this.value == '') this.value='Please describe why';">Please describe why</textarea>
这是一个快乐的好计划! – 2010-05-06 18:52:13
为什么我得到一个减号?! – Karem 2010-05-06 19:02:46
可能因为您既没有包含示例代码也没有提出具体问题(您的“问题”只是您的目标的陈述,因此@Peka的评论)。我重写了你的问题,包括两个。 – Dolph 2010-05-06 19:24:35