display的none与block(判断登录界面的账号密码是否为空)

2023-06-14,,

判断登录界面账号密码是否为空的时候又不想用alert显示就需要用display来隐藏alert啦(在设置时切忌要将隐藏的内容写在账号和密码的div中,否则会根据屏幕的分辨率不同而有所变化,这是本人教训)
    首先要定义账号、密码和隐藏部分的id,即var x=document.getElementById("id"),包括按钮的id,然后点击登录按钮的时候(btn.onclick=function(){具体的选择隐藏和显示的内容})要写函数进行选择隐藏和显示;在重置按钮的是(btn.onclick=function(){清空的内容,包括登录按钮显示的内容})

具体代码如下:

 <html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta charset="UTF-8" />
<title>请登录</title><base target="_blank" /> <style>
body{
margin:0px auto;
background-image: url(images/dlbj1.png) ;
position: relative;
}
.dl{
width: 380px ;
height: 220px ;
background-image: url(images/dl.png) ;
text-align: center ;
margin: auto ;
margin-top: 15% ;
position: center;
}
.btn{
font-family : 微软雅黑 ;
font-size: medium ;
}
#username{
max-width: 200px ;
margin: 10px 0px 0px 0px ;
height: 28px ;
}
#us{
color: #FFFFFF ;
font-family : 微软雅黑;
}
#ps{
color: #FFFFFF ;
font-family : 微软雅黑;
}
#password{
max-width: 200px ;
margin: 20px 0px 10px 0px ;
height: 28px ;
} </style>
</head>
<body>
<form class="dl" method="post">
<div class="dlsy">
<br/><br/><br/>
<div class="username">
账号:<input type="text" id="username"/><span id="u" style="display: none;color: red;font-size:13px;">*请输入账号!</span>
</div>
<div class="password">
密码:<input type="password" id="password"/><span id="p" style="display: none;color: red;font-size:13px;">*请输入密码!</span>
</div>
<div class="empty">
<span id="up" style="display: none;color: red;font-size:13px;">*账号或密码错误</span>
</div>
<div class="btn">
<button type="button" id="reset" class="btn btn-default button-rounded button-small">重置</button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<button type="button" id="submit" class="btn btn-default button-rounded button-small">登录</button>
</div>
</div>
</form> <script type="text/javascript">
var x=document.getElementById("username");
var y=document.getElementById("password");
var btnSubmit = document.getElementById("submit");
var btnReset = document.getElementById("reset");
var u=document.getElementById("u");
var p=document.getElementById("p");
var up=document.getElementById("up");
//点击登录按钮时判断账号、密码是否为空或错误
btnSubmit.onclick=function(form){
if(x.value==""){
if(y.value==""){
up.style.display="block";
}
else{
u.style.display="block";
up.style.display="none";
}
}
else if(y.value==""){
if(x.value==""){
up.style.display="block";
}
else{
p.style.display="block";
up.style.display="none";
}
}
else{
u.style.display="none";
up.style.display="block";
p.style.display="none";
} } //点击重置按钮时清空账号密码
btnReset.onclick=function(form){
x.value=""; //清空input里的value
y.value="";
u.style.display="none";
up.style.display="none";
p.style.display="none";
}
/*btn.onclick=function(form){
/*if (x!="admin"||y!=123456){
alert('账号或密码错误!');
}
//判断账号密码是否为空则弹出
if (""==x) {
alert("请输入账号!");
flag=false;
return false;
}
else if(""==y){
alert("请输入密码!");
flag=false;
return false;
}
if(flag==true){
flag=true;
}*/ </script>
</body>
</html>

display的none与block(判断登录界面的账号密码是否为空)的相关教程结束。

《display的none与block(判断登录界面的账号密码是否为空).doc》

下载本文的Word格式文档,以方便收藏与打印。