• web 存储


    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
    </head>
    <body>
            <div style="border: 2px dashed #ccc;320px;text-align:center;">     
                    <label for="sitename">网站名(key):</label>  
                    <input type="text" id="sitename" name="sitename" class="text"/>  
                    <br/>  
                    <label for="siteurl">网 址(value):</label>  
                    <input type="text" id="siteurl" name="siteurl"/>  
                    <br/>  
                    <input type="button" onclick="save()" value="新增记录"/>  
                    <hr/>  
                    <label for="search_phone">输入网站名:</label>  
                    <input type="text" id="search_site" name="search_site"/>  
                    <input type="button" onclick="find()" value="查找网站"/>  
                    <p id="find_result"><br/></p>  
                </div>  
                <br/>  
                <div id="list">  
                </div>  
                <script>
                // 载入所有存储在localStorage的数据
                loadAll();     
                    
                //保存数据  
                function save(){  
                    var siteurl = document.getElementById("siteurl").value;  
                    var sitename = document.getElementById("sitename").value;  
                    localStorage.setItem(sitename, siteurl);
                    alert("添加成功");
                }
                //查找数据  
                function find(){  
                    var search_site = document.getElementById("search_site").value;  
                    var siteurl = localStorage.getItem(search_site);  
                    var find_result = document.getElementById("find_result");  
                    find_result.innerHTML = search_site + "的网址是:" + siteurl;  
                }
                //将所有存储在localStorage中的对象提取出来,并展现到界面上
                function loadAll(){  
                    var list = document.getElementById("list");  
                    if(localStorage.length>0){  
                        var result = "<table border='1'>";  
                        result += "<tr><td>key</td><td>value</td></tr>";  
                        for(var i=0;i<localStorage.length;i++){  
                            var sitename = localStorage.key(i);  
                            var siteurl = localStorage.getItem(sitename);  
                            result += "<tr><td>"+sitename+"</td><td>"+siteurl+"</td></tr>";  
                        }  
                       // result += "</table>";  
                        list.innerHTML = result;  
                    }else{  
                        list.innerHTML = "数据为空……";  
                    }  
                }      
                </script>
    </body>
    </html>
    
    Json处理
    <!DOCTYPE html>
    <html>  
    <head>  
        <meta charset="utf-8">  
        <title>HTML5本地存储之Web Storage篇</title>  
    </head>  
    <body>  
        <div style="border: 2px dashed #ccc;320px;text-align:center;">
            <label for="keyname">别名(key):</label>  
            <input type="text" id="keyname" name="keyname" class="text"/>  
            <br/>  
            <label for="sitename">网站名:</label>  
            <input type="text" id="sitename" name="sitename" class="text"/>  
            <br/>  
            <label for="siteurl">网 址:</label>  
            <input type="text" id="siteurl" name="siteurl"/>  
            <br/>  
            <input type="button" onclick="save()" value="新增记录"/>  
            <hr/>  
            <label for="search_phone">输入别名(key):</label>  
            <input type="text" id="search_site" name="search_site"/>  
            <input type="button" onclick="find()" value="查找网站"/>  
            <p id="find_result"><br/></p>  
        </div>  
        <br/>  
        <div id="list">  
        </div>  
        <script>
        //保存数据  
        function save(){  
            var site = new Object;
            site.keyname = document.getElementById("keyname").value;
            site.sitename = document.getElementById("sitename").value;  
            site.siteurl = document.getElementById("siteurl").value;
            var str = JSON.stringify(site); // 将对象转换为字符串
            localStorage.setItem(site.keyname,str);  
            alert("保存成功");
        }  
        //查找数据  
        function find(){  
            var search_site = document.getElementById("search_site").value;  
            var str = localStorage.getItem(search_site);  
            var find_result = document.getElementById("find_result");
            var site = JSON.parse(str);  
            find_result.innerHTML = search_site + "的网站名是:" + site.sitename + ",网址是:" + site.siteurl;  
        }  
        
        //将所有存储在localStorage中的对象提取出来,并展现到界面上
        // 确保存储的 keyname 对应的值为转换对象,否则JSON.parse会报错
        function loadAll(){  
            var list = document.getElementById("list");  
            if(localStorage.length>0){  
                var result = "<table border='1'>";  
                result += "<tr><td>别名</td><td>网站名</td><td>网址</td></tr>";  
                for(var i=0;i<localStorage.length;i++){ 
                    var keyname = localStorage.key(i);  
                    var str = localStorage.getItem(keyname);  
                    var site = JSON.parse(str);  
                    result += "<tr><td>"+site.keyname+"</td><td>"+site.sitename+"</td><td>"+site.siteurl+"</td></tr>";  
                }  
                result += "</table>";  
                list.innerHTML = result;  
            }else{  
                list.innerHTML = "数据为空...";  
            }  
        }  
        </script>
    </body>  
    </html>
    <!-- sessionStorage 对象
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <script>
                function clickCounter()
                {
                    if(typeof(Storage)!=="undefined")
                    {
                        if (localStorage.clickcount)
                        {
                            localStorage.clickcount=Number(localStorage.clickcount)+1;
                        }
                        else
                        {
                            localStorage.clickcount=1;
                        }
                        document.getElementById("result").innerHTML=" 你已经点击了按钮 " + localStorage.clickcount + " 次 ";
                    }
                    else
                    {
                        document.getElementById("result").innerHTML="对不起,您的浏览器不支持 web 存储。";
                    }
                }
                </script>
    </head>
    <body>
            <p><button onclick="clickCounter()" type="button">点我!</button></p>
            <div id="result"></div>
            <p>点击该按钮查看计数器的增加。</p>
            <p>关闭浏览器选项卡(或窗口),重新打开此页面,计数器将继续计数(不是重置)。</p>
     <div id="result"></div>
    <script>
    if(typeof(Storage)!=="undefined")
    {
      localStorage.sitename="菜鸟教程";
      document.getElementById("result").innerHTML="网站名:" + localStorage.sitename;
    }
    else
    {
      document.getElementById("result").innerHTML="对不起,您的浏览器不支持 web 存储。";
    }
    </script>      localStorage.sitename  
    </body>
    </html>
    
    
        localStorage - 用于长久保存整个网站的数据,保存的数据没有过期时间,直到手动去除。
        sessionStorage - 用于临时保存同一窗口(或标签页)的数据,在关闭窗口或标签页之后将会删除这些数据
    
    保存数据:localStorage.setItem(key,value);
    读取数据:localStorage.getItem(key);
    删除单个数据:localStorage.removeItem(key);
    删除所有数据:localStorage.clear();
    得到某个索引的key:localStorage.key(index);
    localStorage.removeItem("sitename");移除sitename
     -->
  • 相关阅读:
    豆瓣还是能学到东西的!
    Mip-Mapping很重要
    果然还是SB了
    Don't Starve,好脚本,好欢乐
    Soft Renderer的乐趣
    java classloader原理深究
    用gitolite搭建git server
    神话设计模式 --开端
    Dive into Spring framework -- 了解基本原理(二)--设计模式-part2
    Dive into Spring framework -- 了解基本原理(二)--设计模式-part1
  • 原文地址:https://www.cnblogs.com/wxy0715/p/12442117.html
Copyright © 2020-2023  润新知