简体   繁体   English

清除textfield JavaScript中的文本

[英]Clear the text in textfield JavaScript

I want to clear the text in textfield, for example if you go to the gmail account signup You can see that text will cleared after you start typing, so simple 我想清除文本字段中的文本,例如,如果你去gmail帐户注册你可以看到你开始输入后文本将被清除,这么简单

document.getElementById("FirstName").value = ""; won't work, word First still going to be over there. 将无法正常工作,字First仍然要在那边。

So how to clear it? 那么如何清除呢? Thank you. 谢谢。

Take a closer look at the HTML. 仔细看看HTML。 The string First isn't a value of the text box but a SPAN that is placed over it via CSS. 字符串First不是文本框的值,而是通过CSS放置在其上的SPAN。

<label id="firstname-label" class="firstname">
  <strong>First name</strong>
  <input id="FirstName" type="text" spellcheck="false" name="FirstName" value="" n="1">
  <span id="firstname-placeholder" class="placeholder-text" style="display: block;">First</span>
</label>

The technique is sometimes called a watermark . 该技术有时被称为水印

As for how to change the value and hide the SPAN 至于如何更改值并隐藏SPAN

As soon as the input has a value (it looks like it is bound on the keyUp event) this inline style is added to the span display: none; 只要输入有一个值(它看起来像是绑定在keyUp事件上),这个内联样式就会被添加到span display: none; . If you wanted to programmatically set input#FirstName and clear the span you would need to either programmatically fire the event that is triggering the hiding or also programmatically hide the span (it looks like each input that has a placeholder value uses the ID <input ID>-placeholder so you could guess the correct span based on the ID of the input you are setting) 如果您想以编程方式设置input#FirstName并清除span,则需要以编程方式触发触发隐藏的事件或以编程方式隐藏span(看起来每个具有占位符值的输入都使用ID <input ID>-placeholder这样你就可以根据你设置的输入的ID来猜测正确的范围了)

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

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