亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
首頁 > web前端 > H5教程 > 正文

HTML5本地存儲應(yīng)用sessionStorage和localStorage

大家講道理
發(fā)布: 2017-08-19 14:13:40
原創(chuàng)
3027人瀏覽過

html5之前,瀏覽器要實現(xiàn)數(shù)據(jù)的存儲,一般都是用cookie,但是cookie有域名和大小限定.?

html5流行之后,可以通過localStorage和sessionStorage實現(xiàn)瀏覽器端的數(shù)據(jù)存儲,這兩者有什么特點呢?

sessionStorage
? ?  sessionStorage屬于臨時會話,數(shù)據(jù)存儲的有效期為:從頁面打開到頁面關(guān)閉的時間段,屬于窗口的臨時存儲,頁面關(guān)閉,本地存儲消失

localStorage

  • 永久存儲(可以手動刪除數(shù)據(jù))

    立即學(xué)習(xí)前端免費學(xué)習(xí)筆記(深入)”;

  • 存儲量限制 ( 5M )

    AppMall應(yīng)用商店
    AppMall應(yīng)用商店

    AI應(yīng)用商店,提供即時交付、按需付費的人工智能應(yīng)用服務(wù)

    AppMall應(yīng)用商店56
    查看詳情 AppMall應(yīng)用商店
  • 客戶端完成,不會請求服務(wù)器處理

  • sessionStorage數(shù)據(jù)在頁面之間不能共享、 而localStorage可以實現(xiàn)頁面之間共享

sessionStorage的應(yīng)用:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <script>
        window.onload = function(){
            var aInput = document.getElementsByTagName('input');
            aInput[0].onclick = function(){
                //sessionStorage: 臨時存儲, 只在當(dāng)前頁面有效,不能傳遞到其他頁面,頁面關(guān)閉之后消失
                window.sessionStorage.setItem("name", aInput[3].value );
            };
            aInput[1].onclick = function(){
                alert(window.sessionStorage.getItem("name" ));
            };
            aInput[2].onclick = function(){
                window.sessionStorage.removeItem("name" );
            };
        }
    </script>
</head>
<body>
<input type="button" value="設(shè)置" />
<input type="button" value="獲取" />
<input type="button" value="刪除" />
<br/>
<input type="text" />
</body>
</html>
登錄后復(fù)制

localStorage的應(yīng)用

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <script>
        window.onload = function(){
            var aInput = document.getElementsByTagName('input');
            aInput[0].onclick = function(){
                //localStorage : 永久性存儲
                window.localStorage.setItem("name", aInput[3].value);
                window.localStorage.setItem("name2", 'aaaaa');
            };
            aInput[1].onclick = function(){
                alert( window.localStorage.getItem( "name" ) );
                alert( window.localStorage.getItem( "name2" ) );
            };
            aInput[2].onclick = function(){
                window.localStorage.removeItem("name");
//                window.localStorage.clear();
            };
        }
    </script>
</head>
<body>
<input type="button" value="設(shè)置" />
<input type="button" value="獲取" />
<input type="button" value="刪除" />
<br/>
<input type="text" />
</body>
</html>
登錄后復(fù)制
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <script>
        window.onload = function () {
            var aInput = document.getElementsByTagName("input");
            var oT = document.querySelector("textarea");

            if (window.localStorage.getItem("userName")) {
                aInput[0].value = window.localStorage.getItem("userName");
            }

            for (var i = 0; i < aInput.length; i++) {
                if (window.localStorage.getItem('sex') == aInput[i].value) {
                    aInput[i].checked = true;
                }
            }

            if (window.localStorage.getItem("note")) {
                oT.value = window.localStorage.getItem("note");
            }

            window.onunload = function () {
                if (aInput[0].value) {
                    window.localStorage.setItem("userName", aInput[0].value);
                }

                for (var i = 0; i < aInput.length; i++) {
                    if (aInput[i].checked == true) {
                        window.localStorage.setItem('sex', aInput[i].value);
                    }
                }

                if (oT.value) {
                    window.localStorage.setItem('note', oT.value);
                }
            }
        }
    </script>
</head>
<body>
<p>
    用戶名: <input type="text"/>
</p>

<p>
    性別: <br/>
    <input type="radio" name="sex" value="男"/>男
    <input type="radio" name="sex" value="女"/>女
</p>

<p>
    備注:
    <textarea cols="30" rows="10"></textarea>
</p>

</body>
</html>
登錄后復(fù)制


以上就是HTML5本地存儲應(yīng)用sessionStorage和localStorage的詳細內(nèi)容,更多請關(guān)注php中文網(wǎng)其它相關(guān)文章!

相關(guān)標(biāo)簽:
HTML速學(xué)教程(入門課程)
HTML速學(xué)教程(入門課程)

HTML怎么學(xué)習(xí)?HTML怎么入門?HTML在哪學(xué)?HTML怎么學(xué)才快?不用擔(dān)心,這里為大家提供了HTML速學(xué)教程(入門課程),有需要的小伙伴保存下載就能學(xué)習(xí)啦!

下載
來源:php中文網(wǎng)
本文內(nèi)容由網(wǎng)友自發(fā)貢獻,版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請聯(lián)系admin@php.cn
作者最新文章
最新問題
開源免費商場系統(tǒng)廣告
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
關(guān)于我們 免責(zé)申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長!
關(guān)注服務(wù)號 技術(shù)交流群
PHP中文網(wǎng)訂閱號
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號