English 中文(简体)
如何用javascript明确投入?
原标题:HTML how to clear input using javascript?

I have this INPUT, it will clear everytime we click inside of it.

The problem: I want to clear only if value = [email protected]

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

Can someone help me to do this? I don t know how to compare, I already tried but no success.

最佳回答
<script type="text/javascript">
    function clearThis(target) {
        if (target.value ==  [email protected] ) {
            target.value = "";
        }
    }
</script>

你们是否真心想?

问题回答

For me this is the best way:

<form id="myForm">
  First name: <input type="text" name="fname" value="Demo"><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>

页: 1

<input type="text" name="email" placeholder="[email protected]" size="30" />

or try this for older browsers

<input type="text" name="email" value="[email protected]" size="30" onblur="if(this.value==  ){this.value= [email protected] ;}" onfocus="if(this.value== [email protected] ){this.value=  ;}">

你们可以使用一个持位人,因为你这样做是为了你,而对于那些不支持持单人的旧浏览器来说,这样做是为了:

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

CODE EXPLAINED:当文本箱集中时,明确了价值。 当盒子没有重点时,当盒子没有空白时,则取而代之。

我希望,我的工作是一样的,但当时我尝试了这一问题,并为我工作。

引证:

<script type="text/javascript">
function clearThis(target){
    if(target.value == "[email protected]")
    {
        target.value= "";
    }
}
</script>

<script type="text/javascript">
    function clearThis(target){
        if (target.value === "[email protected]") {
            target.value= "";
        }
    }
    </script>
<input type="text" name="email" value="[email protected]" size="30" onfocus="clearThis(this)">

Try it out here:

而不是删除名称文本的使用placeholder <>/strong>,而是将其称为良好做法。

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

我对所有这些答复感到惊讶,没有人提到最简单、最现代的方式这样做:

<input 
    type="text" 
    placeholder="Your Name" 
    onfocus="this.placeholder=  " 
    onblur="this.placeholder= Your Name "
>

The onblur is only necessary if you want to restore the original placeholder after the user clicks away from the input.





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!