[英]HTML how to clear input using javascript?
我有这个输入,每次我们点击它时它都会清除。
问题:我只想在 value = exemlo@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)">
有人可以帮我做到这一点吗? 我不知道如何比较,我已经尝试过但没有成功。
<script type="text/javascript">
function clearThis(target) {
if (target.value == 'exemplo@exemplo.com') {
target.value = "";
}
}
</script>
这真的是你要找的吗?
对我来说,这是最好的方法:
<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>
您可以使用属性placeholder
<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='';}">
您可以使用占位符,因为它适合您,但对于不支持占位符的旧浏览器,请尝试以下操作:
<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)" />
代码解释:当文本框获得焦点时,清除该值。 当文本框未聚焦且框为空白时,替换值。
我希望这有效,我一直有同样的问题,但后来我尝试了这个,它对我有用。
你不需要为此烦恼。 随便写
<input type="text" name="email" placeholder="exemplo@exemplo.com" size="30">
用占位符替换值
而不是清除名称文本使用占位符属性,这是一种很好的做法
<input type="text" placeholder="name" name="name">
试试这个 :
<script type="text/javascript">
function clearThis(target){
if(target.value == "exemplo@exemplo.com")
{
target.value= "";
}
}
</script>
<script type="text/javascript">
function clearThis(target){
if (target.value === "exemplo@exemplo.com") {
target.value= "";
}
}
</script>
<input type="text" name="email" value="exemplo@exemplo.com" size="30" onfocus="clearThis(this)">
在这里试试: http : //jsfiddle.net/2K3Vp/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.