HTML如何使用javascript清除输入?

HTML如何使用javascript清除输入?,javascript,html,input,erase,Javascript,Html,Input,Erase,我有这个输入,它会清除每次我们点击里面 问题是: 仅当值=exemplo@exemplo.com <script type="text/javascript"> function clearThis(target) { target.value= ""; } </script> <input type="text" name="email" value="exemplo@exemplo.com" size="30" onfocus="

我有这个输入,它会清除每次我们点击里面

问题是: 仅当值=exemplo@exemplo.com

<script type="text/javascript">
    function clearThis(target) {
        target.value= "";
    }
</script>
<input type="text" name="email" value="exemplo@exemplo.com" size="30" onfocus="clearThis(this)">

函数clearThis(目标){
target.value=“”;
}
有人能帮我做这个吗? 我不知道如何比较,我已经试过了,但没有成功。


<script type="text/javascript">
    function clearThis(target) {
        if (target.value == 'exemplo@exemplo.com') {
            target.value = "";
        }
    }
</script>
函数clearThis(目标){ 如果(target.value=='exemplo@exemplo.com') { target.value=“”; } }
这真的是你想要的吗?

试试这个:

<script type="text/javascript">
function clearThis(target){
    if(target.value == "exemplo@exemplo.com")
    {
        target.value= "";
    }
}
</script>

函数clearThis(目标){
如果(target.value==”exemplo@exemplo.com")
{
target.value=“”;
}
}


函数clearThis(目标){
如果(target.value==”exemplo@exemplo.com") {
target.value=“”;
}
}

在这里尝试一下:

您可以使用属性
占位符

<input type="text" name="email" placeholder="exemplo@exemplo.com" size="30" />

或者在较旧的浏览器上试试这个

<input type="text" name="email" value="exemplo@exemplo.com" size="30" onblur="if(this.value==''){this.value='exemplo@exemplo.com';}" onfocus="if(this.value=='exemplo@exemplo.com'){this.value='';}">

你不必为此烦恼。只要写

<input type="text" name="email" placeholder="exemplo@exemplo.com" size="30">


将该值替换为占位符

您可以使用占位符,因为它可以为您使用占位符,但对于不支持占位符的旧浏览器,请尝试以下操作:

<script>
function clearThis(target) {
    if (target.value == "exemplo@exemplo.com") {
        target.value = "";
    }
}
function replace(target) {
    if (target.value == "" || target.value == null) {
        target.value == "exemplo@exemplo.com";
    }
}
</script>
<input type="text" name="email" value="exemplo@exemplo.com" size="x" onfocus="clearThis(this)" onblur="replace(this)" />

函数clearThis(目标){
如果(target.value==”exemplo@exemplo.com") {
target.value=“”;
}
}
功能替换(目标){
if(target.value==“”| | target.value==null){
target.value==”exemplo@exemplo.com";
}
}
代码说明:当文本框有焦点时,清除该值。当文本框未聚焦且为空时,替换该值


我希望这能起作用,我也遇到过同样的问题,但后来我尝试了这个方法,它对我起了作用。

使用占位符属性而不是清除名称文本是一种很好的做法

<input type="text" placeholder="name"  name="name">

对我来说,这是最好的方式:

<form id="myForm">
  First name: <input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"><br><br>
  <input type="button" onclick="myFunction()" value="Reset form">
</form>

<script>
function myFunction() {
    document.getElementById("myForm").reset();
}
</script>

名字:
姓氏:

函数myFunction(){ document.getElementById(“myForm”).reset(); }
您正在查找
占位符
属性。IE8本身不支持
占位符
属性,那么IE 9及更高版本又如何?欢迎使用!请考虑添加一个解释为什么你认为这是OP问题的一个解决方案,因为它涉及在特定条件下重置输入值。这一点尤其重要,因为其他答案已经涵盖了这个问题。一定要看看答案
<form id="myForm">
  First name: <input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"><br><br>
  <input type="button" onclick="myFunction()" value="Reset form">
</form>

<script>
function myFunction() {
    document.getElementById("myForm").reset();
}
</script>