使用javascript实现简易登录功能

新建一个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;
}
一些背景图片可以用你自己的图片来取代,用自己想用的图片路径

好啦,以上就是一个简易(不需要数据库)的登录系统,希望能给大家一些参考,如有问题可以在评论区留言,也很希望大佬来给我提出更多改进的意见,谢谢大家

  • 11
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,以下是一个简单的JavaScript程序实现简易购物车的示例: ```javascript // 初始化商品列表 const products = [ { id: 1, name: '商品1', price: 100 }, { id: 2, name: '商品2', price: 200 }, { id: 3, name: '商品3', price: 300 }, { id: 4, name: '商品4', price: 400 }, ] // 初始化购物车 let cart = [] // 添加商品到购物车 function addToCart(id) { const product = products.find(p => p.id === parseInt(id)) if (product) { cart.push(product) console.log(`已添加商品 ${product.name} 到购物车`) } else { console.log('商品不存在') } } // 从购物车中移除商品 function removeFromCart(id) { const index = cart.findIndex(p => p.id === parseInt(id)) if (index !== -1) { const product = cart.splice(index, 1)[0] console.log(`已从购物车中移除商品 ${product.name}`) } else { console.log('购物车中不存在该商品') } } // 计算购物车总价 function getTotalPrice() { return cart.reduce((total, product) => total + product.price, 0) } // 打印购物车商品列表和总价 function printCart() { console.log('购物车商品列表:') cart.forEach(product => console.log(`- ${product.name}(${product.price}元)`)) console.log(`总价:${getTotalPrice()}元`) } // 测试 addToCart(1) addToCart(2) addToCart(3) removeFromCart(2) printCart() ``` 在这个示例中,我们首先定义了一个商品列表和一个购物车数组,然后实现了三个操作函数: - `addToCart(id)`:将指定id的商品添加到购物车中; - `removeFromCart(id)`:从购物车中移除指定id的商品; - `getTotalPrice()`:计算购物车中所有商品的总价。 最后,我们调用了这些函数来测试程序的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值