帶你用原生js寫一個簡單的注冊登錄頁面
1.首先是我們的注冊頁面
這是我們的html骨架
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>注冊</title>
</head>
<link rel="stylesheet" href="./css/common.css" rel="external nofollow" >
<style>
body {
width: 100%;
height: 100%;
background-image: url(./img/RE53tTA.jfif);
background-repeat: no-repeat;
background-size: cover;
}
</style>
<body>
<div class="header">
<h2>注冊</h2>
</span>
<label for="username"><span>用戶名:</span><input type="text" id="use"></label>
<br>
<label for="password"><span>密碼:</span><input type="password" id="pwd"></label>
<br>
<label for="password"><span>確認密碼 </span><input type="password" id="pwd2"> </label>
<br>
<div>
<button onclick="login()">注冊</button>
<button><a href="./登錄.html" rel="external nofollow" >去登錄</a></button>
</div>
</div>
</body>
</html>這是css樣式
* {
margin: 0;
padding: 0;
}
a {
text-decoration: none;
color: #fff;
font-size: 18px;
}
.header {
width: 400px;
height: 450px;
background: rgba(0, 0, 0, 0.2);
border-radius: 14px;
display: flex;
flex-direction: column;
margin: 100px 0 0 200px;
padding: 20px;
}
.header h2 {
font-size: 24px;
margin-top: 15px;
color: rgba(255, 255, 255, 0.7);
}
.header > label {
margin-top: 40px;
width: 350px;
display: flex;
align-items: center;
justify-content: space-between;
}
.header > label span {
font-size: 24px;
color: #fff;
}
.header > label input {
border-radius: 20px;
border: 1px solid #ccc;
padding: 0 20px;
background-color: rgba(255, 255, 255, 0.6);
box-sizing: border-box;
outline: none;
width: 240px;
height: 30px;
font-size: 18px;
}
.header > div {
margin-top: 30px;
display: flex;
justify-content: space-around;
width: 325px;
}
.header > div button {
width: 100px;
height: 30px;
background: rgba(0, 0, 0, 0.5);
border: none;
border-radius: 12px;
font-size: 18px;
color: #fff;
}下面就是我們注冊的js封裝了
這里的函數(shù)調(diào)用直接寫到了html里面的button事件上面了
<script>
//獲取用戶名;
let useA = localStorage.getItem('username');
console.log(useA);
//封裝注冊方法
function login() {
//獲取事件的value
let username = document.querySelector('#use').value;
let password = document.querySelector('#pwd').value;
//這里調(diào)用存入本地的數(shù)據(jù)
Date(username, password);
}
//將數(shù)據(jù)存入本地
function Date(username, password) {
localStorage.setItem(username, JSON.stringify({
username,
password,
tag: false
}))
if (username === '' || password === '') {
alert('請先注冊');
}
//判斷兩次密碼是否一樣
else if (pwd.value != pwd2.value) {
//不一樣的話重新輸入
alert('兩次輸入密碼不一樣 請重新輸入');
} else {
//一樣則提示注冊成功
alert('注冊成功 ! ! !')
}
}
</script>這就是我的注冊頁面 ,當然各位要是覺得不好看也可以換成自己喜歡的圖片;

2.登陸頁面
到這里當然要寫我們的登錄頁面了,從注冊頁面獲取數(shù)據(jù) 然后調(diào)用
html樣式
<body>
<div class="header">
<h2>登錄</h2>
<label for="username"><span>用戶名:</span><input type="text" id="use"></label>
<br>
<label for="password"><span>密碼:</span><input type="password" id="pwd"></label>
<br>
<div class="pwsd">
<input type="checkbox" id="cbx"><span>記住密碼</span>
</div>
<div class="del">
<button onclick="login()">登錄</button>
<button><a href="./注冊.html" rel="external nofollow" >去注冊</a></button>
</div>
</div>
</body>css樣式
<style>
a {
text-decoration: none;
color: #fff;
}
body {
width: 100%;
height: 100%;
background-image: url(./img/RE53r3l.jfif);
background-repeat: no-repeat;
background-size: cover;
}
.header {
width: 400px;
height: 450px;
background: rgba(0, 0, 0, .2);
border-radius: 14px;
display: flex;
flex-direction: column;
margin: 100px 0 0 200px;
padding: 20px;
}
h2 {
font-size: 24px;
color: #fff;
}
label {
margin-top: 40px;
width: 350px;
display: flex;
align-items: center;
justify-content: space-between;
}
label>span {
font-size: 24px;
color: #fff;
}
label>input {
border-radius: 20px;
border: 1px solid #ccc;
padding: 0 20px;
background-color: rgba(255, 255, 255, .6);
box-sizing: border-box;
outline: none;
width: 240px;
height: 30px;
font-size: 18px;
}
.del {
margin-top: 30px;
display: flex;
justify-content: space-around;
width: 325px;
}
.pwsd {
display: flex;
margin-top: 45px;
}
.pwsd>input {
width: 24px;
height: 24px;
}
.pwsd>span {
font-size: 18px;
color: #fff;
margin-left: 20px;
}
button {
width: 100px;
height: 40px;
background: rgba(0, 0, 0, .6);
border: none;
border-radius: 12px;
font-size: 18px;
color: #fff;
}
</style>js樣式
<script>
let use = document.getElementById('use');
let pwd = document.getElementById('pwd');
let cbx = document.getElementById('cbx');
function login() {
//登錄
//取出本地的用戶的值
let dateUse = localStorage.getItem(use.value);
console.log(dateUse)
let dateObj = JSON.parse(dateUse);//將取出的值轉(zhuǎn)化為對象
console.log(dateObj);
if (use.value == dateObj.username && pwd.value == dateObj.password) {
alert('登陸成功');
dateObj.tag = cbx.checked;
localStorage.setItem(use.value, JSON.stringify(dateObj));//再將取出的值轉(zhuǎn)化為字符串類型
location.;//登錄成功則跳轉(zhuǎn)到百度頁面
} else {
alert('用戶名或者密碼錯誤')
}
}
use.onblur = function () {//用戶失去焦點事件s
//取出用戶的值
let res = localStorage.getItem(use.value);
//將用戶值轉(zhuǎn)化為對象
res = JSON.parse(res);
if (res != null && res.tag) {//本地是否有該賬號
cbx.checked = true;
pwd.value = res.password;
}
}
</script>樣品展示:

總結
到此這篇關于用原生js寫一個簡單的注冊登錄頁面的文章就介紹到這了,更多相關原生js寫注冊登錄頁面內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
JavaScript中null和undefined的區(qū)別詳解
null 是一個原始值,表示“無”或“空值”,它通常用于指示變量應有的對象或值不存在,undefined 是一個原始值,表示“未定義”,本文給大家詳細介紹了JavaScript中null和undefined的區(qū)別,需要的朋友可以參考下2024-10-10
最精簡的JavaScript實現(xiàn)鼠標拖動效果的方法
這篇文章主要介紹了最精簡的JavaScript實現(xiàn)鼠標拖動效果的方法,可實現(xiàn)javascript控制鼠標拖動div層效果的方法,需要的朋友可以參考下2015-05-05
小程序開發(fā)中如何使用async-await并封裝公共異步請求的方法
在平常的項目開發(fā)中肯定會遇到同步異步執(zhí)行的問題,這篇文章主要介紹了小程序開發(fā)中如何使用async-await并封裝公共異步請求的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-01-01
身份證號碼前六位所代表的省,市,區(qū), 以及地區(qū)編碼下載
身份證號碼前六位所代表的省,市,區(qū), 以及地區(qū)編碼下載...2007-04-04
JavaScript數(shù)據(jù)結構之鏈表的實現(xiàn)
鏈表是一種常見的數(shù)據(jù)結構。它是動態(tài)地進行存儲分配的一種結構。本文主要介紹JavaScript數(shù)據(jù)結構中鏈表的實現(xiàn),具有很好的參考價值。下面跟著小編一起來看下吧2017-03-03

