JS實(shí)現(xiàn)用戶管理系統(tǒng)
本文實(shí)例為大家分享了JS實(shí)現(xiàn)用戶管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
效果圖:
html代碼:
<h1>新增學(xué)員</h1> ? ? <div class="info"> ? ? ? 姓名:<input type="text" class="uname" /> ? ? ? ?年齡:<input type="text" class="age" /> ? ? ? 性別: ? ? ? <select name="gender" id="" class="gender"> ? ? ? ? <option value="男">男</option> ? ? ? ? <option value="女">女</option> ? ? ? </select> ? ? ? 薪資:<input type="text" class="salary" /> 就業(yè)城市:<select ? ? ? ? name="city" ? ? ? ? id="" ? ? ? ? class="city" ? ? ? > ? ? ? ? <option value="北京">北京</option> ? ? ? ? <option value="上海">上海</option> ? ? ? ? <option value="廣州">廣州</option> ? ? ? ? <option value="深圳">深圳</option> ? ? ? ? <option value="曹縣">曹縣</option> ? ? ? </select> ? ? ? <button class="add">錄入</button> ? ? </div> ? ? ? <h1>就業(yè)榜</h1> ? ? <table> ? ? ? <thead> ? ? ? ? <tr> ? ? ? ? ? <th>學(xué)號(hào)</th> ? ? ? ? ? <th>姓名</th> ? ? ? ? ? <th>年齡</th> ? ? ? ? ? <th>性別</th> ? ? ? ? ? <th>薪資</th> ? ? ? ? ? <th>就業(yè)城市</th> ? ? ? ? ? <th>操作</th> ? ? ? ? </tr> ? ? ? </thead>
css代碼:
* { ? margin: 0; ? padding: 0; } ? a { ? text-decoration: none; ? color:#721c24; } h1 { ? text-align: center; ? color:#333; ? margin: 20px 0; ? } table { ? margin:0 auto; ? width: 800px; ? border-collapse: collapse; ? color:#004085; } th { ? padding: 10px; ? background: #cfe5ff; ?? ? font-size: 20px; ? font-weight: 400; } td,th { ? border:1px solid #b8daff; } td { ? padding:10px; ? color:#666; ? text-align: center; ? font-size: 16px; } tbody tr { ? background: #fff; } tbody tr:hover { ? background: #e1ecf8; } .info { ? width: 900px; ? margin: 50px auto; ? text-align: center; } .info ?input { ? width: 80px; ? height: 25px; ? outline: none; ? border-radius: 5px; ? border:1px solid #b8daff; ? padding-left: 5px; } .info button { ? width: 60px; ? height: 25px; ? background-color: #004085; ? outline: none; ? border: 0; ? color: #fff; ? cursor: pointer; ? border-radius: 5px; } .info .age { ? width: 50px; }
JS代碼:
<script> ? ? ? // 獲取元素 ? ? ? let tbody=document.querySelector(`tbody`) ? ? ? // 錄入按鈕 ? ? ? let add = document.querySelector(`.add`) ? ? ? let stuId=document.querySelector(`.stuId`) //編號(hào) ? ? ? let uname=document.querySelector(`.uname`) //姓名 ? ? ? let age=document.querySelector(`.age`) //年齡 ? ? ? let gender=document.querySelector(`.gender`) //性別 ? ? ? let salary=document.querySelector(`.salary`)//薪資 ? ? ? let city=document.querySelector(`.city`) //所在地區(qū) ? ? ? // 刪除按鈕 ? ? ? let del = document.querySelector(`.del`) ? ? ? ? // ?保存數(shù)據(jù) ? ? ? let arr = JSON.parse(localStorage.getItem(`key`)) || [] ? ? ? ? // ?函數(shù)封裝 ? ? ? ? function init(){ ? ? ? ? ? // ?創(chuàng)建一個(gè)變量,用于拼接 ? ? ? ? let htmlStr=`` ? ? ? ? // ?循環(huán)遍歷 ? ? ? ? for(let i=0;i<arr.length;i++){ ? ? ? ? ? let index= i+1 ? ? ? ? ? // 拼接 ? ? ? ? ? htmlStr +=` ? <tr> ? ? ? ? ? ? ? ? ? ? ? ? ?<td>${index}</td> ? ? ? ? ? ? ? ? ? ? ? ? ?<td>${arr[i].uname}</td> ? ? ? ? ? ? ? ? ? ? ? ? ?<td>${arr[i].age}</td> ? ? ? ? ? ? ? ? ? ? ? ? ?<td>${arr[i].gender}</td> ? ? ? ? ? ? ? ? ? ? ? ? ?<td>${arr[i].salary}</td> ? ? ? ? ? ? ? ? ? ? ? ? ?<td>${arr[i].city}</td> ? ? ? ? ? ? ? ? ? ? ? ? ?<td> ? ? ? ? ? ? ? ? ? ? ? ? ? ?<a href="javascript:" class="del" id='${index}''>刪除</a>? ? ? ? ? ? ? ? ? ? ? ? ? ?</td> ? ? ? ? ? ? ? ? ? ? ? ? </tr> ? ? ? ? ? ? ? ? ? ? ? ? ?` ? ? ? ? } ? ? ? ? // 將拼接的元素插入tbody ? ? ? ? tbody.innerHTML=htmlStr ? ? ? ? } ? ? ? ? // 調(diào)用函數(shù) ? ? ? ? init() ? ? ? ? ? ? // 給錄入按鈕添加點(diǎn)擊事件 ? ? ? add.addEventListener(`click`,function(){ ? ? ? ? // 判斷輸入框是否為空 ? ? ? ? if (uname.value.trim().length ==0){ ? ? ? ? ?alert(`請(qǐng)輸入姓名`) ? ? ? ? ?return ? ? ? ?} ? ? ? ?if (age.value.trim().length ==0){ ? ? ? ? ?alert(`請(qǐng)輸入年齡`) ? ? ? ? ?return ? ? ? ?} ? ? ? ?if (salary.value.trim().length ==0){ ? ? ? ? ?alert(`請(qǐng)輸入薪資`) ? ? ? ? ?return ? ? ? ?} ? ? ? ? let obj={ ? ? ? ? ? //獲取表單元素的value值, ? ? ? ? ? Id:arr.length>0?arr[arr.length-1].stuId+1 :1001, ? ? ? ? ? uname:uname.value, ? ? ? ? ? age:age.value, ? ? ? ? ? gender:gender.value, ? ? ? ? ? salary:salary.value, ? ? ? ? ? city:city.value, ? ? ? ? } ? ? ? ? ? ?// ?將obj追加到數(shù)組的最后 ? ? ? ? ? ?arr.push(obj) ? ? ? ? ? ?// 渲染 ? ? ? ? ? ? ? ?init() ? ? ? ? ? ? // 每次錄入清空輸入框 ? ? ? ? ? uname.value=`` ? ? ? ? ? age.value=`` ? ? ? ? ? salary.value=`` ? ? ? ? ? ?// 存儲(chǔ)數(shù)據(jù) ? ? ? ? ? localStorage.setItem(`key`,JSON.stringify(arr)) ? ? ? }) ? ? ? // 實(shí)現(xiàn)數(shù)據(jù)刪除 ? ? ? // 給tbody點(diǎn)擊事件,事件委托 ? ? ? tbody.addEventListener(`click`,function(e){ ? ? ? ? // 獲取刪除按鈕 ? ? ? ? if (e.target.className ==`del`){ ? ? ? ? ? // 指定刪除arr數(shù)組 ? ? ? ? ? arr.splice(e.target.id-1,1) ? ? ? ? ? // 調(diào)用函數(shù) ? ? ? ? ? init() ? ? ? ? } ? ? ? ? localStorage.setItem(`key`,JSON.stringify(arr)) ? ? ? ?? ? ? ? }) </script>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于BootStrap modal 在IOS9中不能彈出的解決方法(IOS 9 bootstrap modal ios
本文給大家介紹BootStrap modal 在IOS9中不能彈出的問(wèn)題以及bootstrap datepicker 在bootstrap modal中不顯示問(wèn)題的解決方案,非常不錯(cuò),需要的朋友參考下2016-12-12原生javaScript實(shí)現(xiàn)圖片延時(shí)加載的方法
這篇文章主要介紹了原生javaScript實(shí)現(xiàn)圖片延時(shí)加載的方法,無(wú)需通過(guò)載入jQuery腳本即可實(shí)現(xiàn)圖片的延時(shí)加載效果,是非常實(shí)用的技巧,需要的朋友可以參考下2014-12-12如何在一段文字里點(diǎn)一下就可以在里面插入一段文字?
如何在一段文字里點(diǎn)一下就可以在里面插入一段文字?...2007-01-01js 客戶端打印html 并且去掉頁(yè)眉、頁(yè)腳的實(shí)例
下面小編就為大家?guī)?lái)一篇js 客戶端打印html 并且去掉頁(yè)眉、頁(yè)腳的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11解析JavaScript實(shí)現(xiàn)DDoS攻擊原理與保護(hù)措施
本文主要對(duì)JavaScript實(shí)現(xiàn)DDoS攻擊原理與保護(hù)措施進(jìn)行介紹,具有一定的參考價(jià)值,需要的朋友一起來(lái)看下吧2016-12-12Ajax使用原生態(tài)JS驗(yàn)證用戶名是否存在
這篇文章主要為大家詳細(xì)介紹了Ajax使用原生態(tài)JS驗(yàn)證用戶名是否存在的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09