javascript 清除文本框值 onclick 并显示 onblur

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15336316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 00:25:43  来源:igfitidea点击:

Clearing the textbox values onclick and displaying onblur

javascript

提问by Aayush Aarwal

Is there a way to clear the default textbox value onclick on textbox and display onblur of multiple textboxes on form page?

有没有办法清除文本框上的默认文本框值并在表单页面上显示多个文本框的模糊?

采纳答案by Waruna Manjula

function Clear1(str)
{    
   document.getElementById(str).value= "";
}

function Clear2(str2)
{    
var aa1=document.getElementById(str2);
 if (aa1.value==""){  
    document.getElementById(str2).style.backgroundColor = "#ffcccc"; 
 }else{
    document.getElementById(str2).style.backgroundColor = "#ffffff";   
  }
}
<input type="text" value="test1" onClick="Clear1(this.id);" id="textbox1" onblur="Clear2(this.id);">
<input type="text" value="test2" onClick="Clear1(this.id);" id="textbox2" onblur="Clear2(this.id);">
<input type="text" value="test3" onClick="Clear1(this.id);" id="textbox3" onblur="Clear2(this.id);">
<input type="text" value="test4" onClick="Clear1(this.id);" id="textbox4" onblur="Clear2(this.id);">




    

https://jsfiddle.net/warunamanjula/qy0hvmyq/1/

https://jsfiddle.net/warunamanjula/qy0hvmyq/1/

回答by notknown7777

HTML:

HTML:

<input type="text" value="" onClick="Clear();" id="textbox1>
<input type="text" value="" onClick="Clear();" id="textbox2>
<input type="text" value="" onClick="Clear();" id="textbox3>
<input type="text" value="" onClick="Clear();" id="textbox4>

Javascript :

Javascript :

function Clear()
{    
   document.getElementById("textbox1").value= "";
   document.getElementById("textbox2").value= "";
   document.getElementById("textbox3").value= "";
   document.getElementById("textbox4").value= "";
}

Your question was a little vague to me, but the above will clear all the textboxes when one is clicked. Hopefully this helps you.

你的问题对我来说有点含糊,但是当点击一个时,上面会清除所有的文本框。希望这对你有帮助。

回答by Bellash

One Line solution

单线解决方案

  <input type="text" value="" onClick="this.value='';" id="textbox1">

or

或者

 <input type="text" value="" onClick="this.value=='Initial Text'?this.value='':this.value;" id="textbox1">