帶你用原生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>
樣品展示:
總結(jié)
到此這篇關(guān)于用原生js寫一個簡單的注冊登錄頁面的文章就介紹到這了,更多相關(guān)原生js寫注冊登錄頁面內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
經(jīng)典面試題之JavaScript?for循環(huán)(var?let)
如果你也在面試找工作,那么也一定遇到過這道for循環(huán)打印結(jié)果的題,下面我們來探討下,對經(jīng)典面試題之js?for循環(huán)相關(guān)知識感興趣的朋友跟隨小編一起看看吧2023-10-10微信小程序?qū)崿F(xiàn)swiper切換卡內(nèi)嵌滾動條不顯示的方法示例
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)swiper切換卡內(nèi)嵌滾動條不顯示的方法,涉及微信小程序swiper選項卡組件相關(guān)操作技巧,需要的朋友可以參考下2018-12-12IE8的JavaScript點擊事件(onclick)不兼容的解決方法
這篇文章主要介紹了IE8的JavaScript點擊事件(onclick)不兼容的解決方法,大家參考使用吧2013-11-11video.js 實現(xiàn)視頻只能后退不能快進的思路詳解
這篇文章主要介紹了video.js 實現(xiàn)視頻只能后退不能快進的思路詳解,主要思路是點擊進度條需要獲取拖動前的時間點,具體實例代碼大家跟隨小編一起看看吧2018-08-08Javascript下IE與Firefox下的差異兼容寫法總結(jié)
總結(jié)一部分IE和Firefox的javascript差異寫法,對于像書寫多瀏覽器兼容性更好的代碼,可以參考下。2010-06-06在網(wǎng)頁里看flash的trace數(shù)據(jù)的js類
我的js類jdhcn.js中的一個flashDebug方法2009-01-01HTML5基于Tomcat 7.0實現(xiàn)WebSocket連接并實現(xiàn)簡單的實時聊天
WebSocket 是一種自然的全雙工、雙向、單套接字連接。這篇文章給大家介紹了HTML5基于Tomcat 7.0實現(xiàn)WebSocket連接并實現(xiàn)簡單的實時聊天,感興趣的朋友一起學(xué)習(xí)吧2016-10-10Bootbox將后臺JSON數(shù)據(jù)填充Form表單的實例代碼
通過控制器創(chuàng)建一個Index視圖,寫入下列HTML代碼,這里我創(chuàng)建了一個分部視圖,不創(chuàng)建直接寫在同一個頁面也是一樣的效果。這篇文章主要介紹了Bootbox將后臺JSON數(shù)據(jù)填充Form表單 ,需要的朋友可以參考下2018-09-09