[英]Change color of text box when start typing in it rather than click
我在一个正在创建表单并使用JavaScript
验证的网站上工作。 一切进展顺利,但我遇到了问题。
当用户将字段留空并单击“提交”按钮时,表单将显示错误,更改文本框的颜色,并且光标将自动聚焦在该字段上。
但是,我希望当用户开始在该字段中键入内容时,该字段的颜色会自动变回其默认状态。 我希望这项工作可以用JavaScript
完成。 请帮助我,我对此没有任何逻辑。
谢谢
使用onkeydown()
代替onclick()
事件。
此代码可能有效:
This code may work:
<textarea id="txtId" onkeydown="changeColor()"></textarea>
<script>
function changeColor(){
document.querySelector("#txtId").style.backgroundColor="defaultcoloryouwant";
}
</script>
您可以通过以下方式进行操作:
var textinput= document.getElementById("textinput");
textinput.onchange = function() {
if(textinput.value == "")
{
textinput.style.backgroundColor = "red";
}
else
{
textinput.style.backgroundColor = "green";
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.