2010-05-06 189 views
30

给定一个<textarea>有默认值,如下所示:如何清除点击textarea?

<textarea>Please describe why</textarea> 

如何清除,当用户点击编辑的字段的默认值?

+4

这是一个快乐的好计划! – 2010-05-06 18:52:13

+0

为什么我得到一个减号?! – Karem 2010-05-06 19:02:46

+1

可能因为您既没有包含示例代码也没有提出具体问题(您的“问题”只是您的目标的陈述,因此@Peka的评论)。我重写了你的问题,包括两个。 – Dolph 2010-05-06 19:24:35

回答

36

你的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> 
+1

for属性需要匹配一个id,而不是一个名字! (否则它将与单选按钮不兼容) – Quentin 2010-05-06 19:53:01

+1

+1伟大的工作,一个很好的答案。 – 2010-05-06 19:57:47

+0

@David Dorward,你很正确,先生!固定。 – Dolph 2010-05-06 20:04:30

-2
<textarea onClick="javascript: this.value='';">Please describe why</textarea> 
+0

为什么减1,它符合海报的要求。 – user318747 2010-05-06 19:02:25

+2

downvote不是我的,但是这个解决方案每次都会删除文本字段*用户点击它。 – 2010-05-06 19:59:24

5

尝试:

<input name="mytextbox" onfocus="if (this.value=='Please describe why') this.value = ''" type="text" value="Please Describe why"> 
+1

这不是一个textarea,所以-1。这只会删除示例文本,所以+1。 – 2010-05-06 19:29:50

4

你是说像这样的文本字段?

<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> 
+0

为什么我会减去? – tildy 2011-07-25 09:54:27

+0

我的版本只清除默认值。 Dolph的解决方案清除textfield/textarea中的所有内容,不仅是默认值。 – tildy 2011-07-25 10:01:14

0

最好的解决办法是什么我知道,到目前为止:

<textarea name="message" onblur="if (this.value == '') {this.value = 'text here';}" onfocus="if (this.value == 'text here') {this.value = '';}">text here</textarea> 
35

为什么不能简单地用占位符属性?

<textarea placeholder="Please describe why"></textarea> 

这会将您的填充文本置于灰色,当用户单击文本字段时将自动消失。此外,如果失去焦点并且文本区域为空白,它将重新出现。

+5

这仅限于HTML5。 – harrymc 2012-07-04 10:29:24

-1
<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> 
+0

其最佳方式 – Heart 2012-05-05 16:18:19

6

这是你的JavaScript文件:

function yourFunction(){ 
    document.getElementById('yourid').value = ""; 
}; 

这是HTML文件:

<textarea id="yourid" > 
    Your text inside the textarea 
    </textarea> 
    <button onClick="yourFunction();"> 
    Your button Name 
    </button> 
-1

jQuery的属性VAL()应该做的工作。 你可以做$('#YourTextareaID').click(function(e) { e.target.value = ''; })

+1

'val()'是一个jQuery属性,不是纯JS,你需要提一下。 – william44isme 2013-09-08 19:03:53

-1

你可以试试这个代码,

<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>