您好,登錄后才能下訂單哦!
保存數據:localStorage.setItem(key,value);
讀取數據:localStorage.getItem(key);
刪除單個數據:localStorage.removeItem(key);
刪除所有數據:localStorage.clear();
得到某個索引的key:localStorage.key(index);
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style> td {text-align: center;} </style> </head> <body> <label for="">用戶名:</label><input type="text" id="username" /><br> <label for="">密 碼:</label><input type="password" id="password" /><br> <button onclick="btn()">保存</button><button onclick="del()">清空用戶信息</button> <div id="table-box" > <table border="1" cellpadding="0" cellspacing="0" id="table" width="300" > <caption>用戶信息表</caption> <tr> <th>序號</th> <th>用戶名</th> <th>密碼</th> <th>操作</th> </tr> </table> </div> <script> window.onload = function() { //讀取緩存 var str = ""; var Text = localStorage.getItem("T"); Text = JSON.parse(Text); //把字符串轉換成JSON對象 if(Text != null) { //localStorage不為空 show() var objs = Text.te; for(var i = 0; i < objs.length; i++) { str += "<tr><td>" + (i + 1) + "</td><td>" + objs[i].name + "</td><td>" + objs[i].pd + "</td><td><button onclick='del_one("+objs[i].name+")'>刪除</button></td></tr>"; } document.getElementById("table").innerHTML += str; } } function btn() { //存入緩存 var u = document.getElementById("username").value; var b = document.getElementById("password").value; if(u != '' && b != '') { add(u, b) } } function add(a, b) { //設置緩存 var Save = false; var Index = table.rows.length; var Text = localStorage.getItem("T"); Text = JSON.parse(Text); //將一個 JSON 字符串轉換為對象。 if(Text != null && Text != "underfind") { var objs = Text.te; for(var i = 0; i < objs.length; i++) { //遍歷緩存 Save = false; if(objs[i].name == a) { //統計緩存中相同數據個數 alert("用戶名已存在") Save = true; break; } } if(!Save) { //如果條件為真,則執行 var str = "<tr><td>" + (Index) + "</td><td>" + a + "</td><td>" + b + "</td><td><button onclick='del_one("+a+")'>刪除</button></td></tr>"; document.getElementById("table").innerHTML += str; objs[objs.length] = {num: Index,name: a,pd: b}; } } else { show() var str = "<tr><td>" + (Index) + "</td><td>" + a + "</td><td>" + b + "</td><td><button onclick='del_one("+a+")'>刪除</button></td></tr>"; document.getElementById("table").innerHTML += str; var Text = { //要存儲的JSON對象 te: [{num: Index,name: a,pd: b}] } } Text = JSON.stringify(Text); //將JSON對象轉化成字符串 localStorage.setItem("T", Text); //用localStorage保存轉化好的的字符串 document.getElementById("username").value = ''; document.getElementById("password").value = ''; } function del() { //清空緩存 localStorage.clear(); window.location.reload(); } function show(){ document.getElementById("table-box").style.display ="block"; } function del_one(event){ var Text = localStorage.getItem("T"); Text = JSON.parse(Text); //把字符串轉換成JSON對象 var objs = Text.te; for(var i = 0; i < objs.length; i++) { if(objs[i].name == event) { //說明存在該用戶 objs.splice(i, 1); //刪除某一條數據 Text = JSON.stringify(Text); //將JSON對象轉化成字符串 localStorage.setItem("T", Text); //用localStorage保存轉化好的的字符串 } } if(objs.length == "0"){localStorage.clear();} window.location.reload(); } </script> </body> </html>
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。