1. 程式人生 > >web存儲

web存儲

web存儲

localStorage 對象存儲的數據沒有時間限制。數據以 鍵/值 對存在, web網頁的數據只允許該網頁訪問使用。

  • 保存數據:localStorage.setItem(key,value);

  • 讀取數據:localStorage.getItem(key);

  • 刪除單個數據:localStorage.removeItem(key);

  • 刪除所有數據:localStorage.clear();

  • 得到某個索引的key:localStorage.key(index);

將用戶名和密碼保存到localstorage中,在頁面展示

<!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="">密&emsp;碼:</label><input type="password" id="password" /><br>
        <button  onclick="btn()">保存</button><button onclick="del()">清空用戶信息</button>
        <div id="table-box" style="display: none;">
            <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>


web存儲