帶你用原生js寫(xiě)一個(gè)簡(jiǎn)單的注冊(cè)登錄頁(yè)面
1.首先是我們的注冊(cè)頁(yè)面
這是我們的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>注冊(cè)</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>注冊(cè)</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>確認(rèn)密碼 </span><input type="password" id="pwd2"> </label>
<br>
<div>
<button onclick="login()">注冊(cè)</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;
}下面就是我們注冊(cè)的js封裝了
這里的函數(shù)調(diào)用直接寫(xiě)到了html里面的button事件上面了
<script>
//獲取用戶名;
let useA = localStorage.getItem('username');
console.log(useA);
//封裝注冊(cè)方法
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('請(qǐng)先注冊(cè)');
}
//判斷兩次密碼是否一樣
else if (pwd.value != pwd2.value) {
//不一樣的話重新輸入
alert('兩次輸入密碼不一樣 請(qǐng)重新輸入');
} else {
//一樣則提示注冊(cè)成功
alert('注冊(cè)成功 ! ! !')
}
}
</script>這就是我的注冊(cè)頁(yè)面 ,當(dāng)然各位要是覺(jué)得不好看也可以換成自己喜歡的圖片;

2.登陸頁(yè)面
到這里當(dāng)然要寫(xiě)我們的登錄頁(yè)面了,從注冊(cè)頁(yè)面獲取數(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="./注冊(cè).html" rel="external nofollow" >去注冊(cè)</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)化為對(duì)象
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)到百度頁(yè)面
} else {
alert('用戶名或者密碼錯(cuò)誤')
}
}
use.onblur = function () {//用戶失去焦點(diǎn)事件s
//取出用戶的值
let res = localStorage.getItem(use.value);
//將用戶值轉(zhuǎn)化為對(duì)象
res = JSON.parse(res);
if (res != null && res.tag) {//本地是否有該賬號(hào)
cbx.checked = true;
pwd.value = res.password;
}
}
</script>樣品展示:

總結(jié)
到此這篇關(guān)于用原生js寫(xiě)一個(gè)簡(jiǎn)單的注冊(cè)登錄頁(yè)面的文章就介紹到這了,更多相關(guān)原生js寫(xiě)注冊(cè)登錄頁(yè)面內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
微信小程序時(shí)間軸實(shí)現(xiàn)方法示例
這篇文章主要介紹了微信小程序時(shí)間軸實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了微信小程序wx:for語(yǔ)句使用與時(shí)間軸縱向布局相關(guān)操作技巧,需要的朋友可以參考下2019-01-01
前端防止服務(wù)器過(guò)載實(shí)現(xiàn)限流的常見(jiàn)策略和技術(shù)指南
前端限流是一種控制請(qǐng)求頻率的技術(shù),旨在防止過(guò)多的請(qǐng)求在同一時(shí)間段內(nèi)發(fā)送到服務(wù)器,下面小編就為大家簡(jiǎn)單講講幾種常見(jiàn)的策略和技術(shù)吧2025-11-11
JavaScript中null和undefined的區(qū)別詳解
null 是一個(gè)原始值,表示“無(wú)”或“空值”,它通常用于指示變量應(yīng)有的對(duì)象或值不存在,undefined 是一個(gè)原始值,表示“未定義”,本文給大家詳細(xì)介紹了JavaScript中null和undefined的區(qū)別,需要的朋友可以參考下2024-10-10
最精簡(jiǎn)的JavaScript實(shí)現(xiàn)鼠標(biāo)拖動(dòng)效果的方法
這篇文章主要介紹了最精簡(jiǎn)的JavaScript實(shí)現(xiàn)鼠標(biāo)拖動(dòng)效果的方法,可實(shí)現(xiàn)javascript控制鼠標(biāo)拖動(dòng)div層效果的方法,需要的朋友可以參考下2015-05-05
js動(dòng)態(tài)拼接正則表達(dá)式的兩種方法
這篇文章主要介紹了js動(dòng)態(tài)拼接正則表達(dá)式的兩種方法,需要的朋友可以參考下2014-03-03
小程序開(kāi)發(fā)中如何使用async-await并封裝公共異步請(qǐng)求的方法
在平常的項(xiàng)目開(kāi)發(fā)中肯定會(huì)遇到同步異步執(zhí)行的問(wèn)題,這篇文章主要介紹了小程序開(kāi)發(fā)中如何使用async-await并封裝公共異步請(qǐng)求的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
身份證號(hào)碼前六位所代表的省,市,區(qū), 以及地區(qū)編碼下載
身份證號(hào)碼前六位所代表的省,市,區(qū), 以及地區(qū)編碼下載...2007-04-04
JavaScript數(shù)據(jù)結(jié)構(gòu)之鏈表的實(shí)現(xiàn)
鏈表是一種常見(jiàn)的數(shù)據(jù)結(jié)構(gòu)。它是動(dòng)態(tài)地進(jìn)行存儲(chǔ)分配的一種結(jié)構(gòu)。本文主要介紹JavaScript數(shù)據(jù)結(jié)構(gòu)中鏈表的實(shí)現(xiàn),具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-03-03

