使用javascript实现简易登录功能
新建一个js文件(我这里取名为Signin.js),定义两个变量,用户名和密码,使用从网页中输入的参数进行赋值,之后进行if判断语句,如果输入的用户名和密码都匹配正确则弹窗登陆成功,并且进入指定网页,如果用户名或者密码不正确,则弹窗用户名或密码错误,如果用户名未输入,则弹窗用户名不能为空,如果密码未输入,则弹窗密码为空,js代码如下
新建一个js文件(我这里取名为Signin.js),定义两个变量,用户名和密码,使用从网页中输入的参数进行赋值,之后进行if判断语句,如果输入的用户名和密码都匹配正确则弹窗登陆成功,并且进入指定网页,如果用户名或者密码不正确,则弹窗用户名或密码错误,如果用户名未输入,则弹窗用户名不能为空,如果密码未输入,则弹窗密码为空,js代码如下(复制可直接使用):
function login() {
var userName = document.getElementById("nName").value;
var userPass = document.getElementById("uPass").value;
if (userPass == "123456" && userName == "root") {
alert("登入成功");
window.location.href="index.html";
}
else if (userName == "" || userName == null) {
alert("用户名不能为空");
return false;
}
else if (userPass == "" || userPass == null) {
alert("密码不能为空");
return false;
}
else if (userPass != "123456" || userName != "mhh") {
alert("用户名或密码错误");
return false;
}
else{
return true;
}
}
之后在你的html文件中导入该文件
<script src="Signin.js"></script>
html文件中body标签下的登录窗口需要有用户输入栏,我是这样写的(也可直接复制)
<body style="text-align: center;background-image:url('JPG/denglu.jpg');" onLoad="document.getElementById('enter').focus()" οnkeydοwn="if(event.keyCode===13) document.all.enter.click()"><br><br><br><br><br>
#此处导入CSS文件
<style>
@import url(denglu.css);
</style>
<div style="text-align: center;width: 100%;height: 50px;color: #fff;font-family: '楷体';font-size: 30px;text-shadow: -2px 2px 0 skyblue,2px 2px 0 skyblue,2px -2px 0 skyblue;"><h1>登录</h1></div>
<div class="content" >
<div class="login-wrap">
<form id="user_login" action="">
<h3 style="font-family: '楷体';">登 录</h3><br>
账号:<input class="name" name="name" id="nName" type="text" placeholder="请输入用户名" ><br>
密码:<input class="code" name="password" id="uPass" type="password" placeholder="请输入密码" >
<div class="btn" onLoad="document.getElementById('enter').focus()">
<input type="button" id="enter" class="submit" value="登录" οnclick="login()">
<input type="reset" id="reset" class="reset" value="重置" >
</div>
<div id="CheckMsg" class="msg"></div>
</form>
</div>
</div>
</body>
注意:CSS样式需要定义来美化用户页面
我的CSS文件如下:
.content{
padding:0 auto;
margin: 0 auto;
height: 450px;
width: 100%;
background-size:100% 450px ;
margin-top: 25px;
}
.login-wrap{
position: absolute;
width:320px;
height: 300px;
border-radius: 10px;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
right:40%;
margin-top: 75px;
background: url("../Image/Login-Img/form_bg.png") no-repeat;
background-size: 100%;
border-radius: 25px;
background-color: #fff;
opacity: 0.8;
}
.login-wrap h3{
color:black;
font-size: 18px;
text-align: center;
}
.name,.code{
border:1px solid cyan;
width:130px;
height: 40px;
margin-left: 25px;
margin-bottom: 20px;
padding-left: 40px;
border-radius:12px;
border-color:skyblue;
}
.name{
color: black;
background-position-x:12px;
}
.code{
color: blue;
background-position-x:12px;
}
.btn input{
height: 40px;
width: 120px;
float: left;
margin-right: 25px;
border:none;
color:black;
font-size: 16px;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
margin-top: 10px;
cursor: pointer;
}
input:active{border-color:#147a62}
.submit{background: #ea8c37;margin-left: 25px;}
.reset{background: #bbb;}
/**错误信息提醒**/
.msg{
color: #ea8c37;
font-size: 14px;
padding-left: 40px;
padding-top: 10px;
clear: both;
font-weight: bold;
}
一些背景图片可以用你自己的图片来取代,用自己想用的图片路径
好啦,以上就是一个简易(不需要数据库)的登录系统,希望能给大家一些参考,如有问题可以在评论区留言,也很希望大佬来给我提出更多改进的意见,谢谢大家
更多推荐
所有评论(0)