什麼是html5的本機儲存功能

青灯夜游
發布: 2022-01-23 11:23:30
原創
2076 人瀏覽過

在html5中,本地儲存是一種讓網頁可以把鍵值對儲存在使用者瀏覽器客戶端的方法。透過本地存儲,web應用程式能夠在用戶瀏覽器中對資料進行本地的存儲。

什麼是html5的本機儲存功能

本教學操作環境:windows7系統、HTML5版、Dell G3電腦。

什麼是 HTML 本機儲存?

本地儲存(LocalStorage)是一種讓網頁可以把鍵值對儲存在使用者瀏覽器客戶端的方法;透過本地存儲,web 應用程式能夠在使用者瀏覽器中對資料進行本地的儲存。

html本地儲存:優於cookies

在 HTML5 之前,應用程式資料只能儲存在 cookie 中,包括每個伺服器請求。本機儲存則較安全,並且可在不影響網站效能的前提下將大量資料儲存於本機。

與 cookie 不同,儲存限制要大得多(至少5MB),且資訊不會傳送到伺服器。

本地儲存經由起源地(origin)(經由域和協定)。所有頁面,從起源地,能夠儲存和存取相同的資料。

關於html5的本機儲存物件:

window.localStorage 儲存永久資料

window.sessionStorage 針對一個session 來儲存資料(當瀏覽器關閉,儲存的資料將會清除)

#模擬淘寶搜索,對本地資料進行儲存?

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #all {
            width: 600px;
            margin: 100px auto 0px;
            position: relative;
        }

        #all input {
            float: left;
            width: 500px;
            height: 30px;
            outline: none;
            text-indent: 5px;
            border-radius: 15px 0px 0px 15px;
            border: 1px solid #ccc;
        }

        #all button {
            float: left;
            width: 80px;
            height: 32px;
            border: none;
            color: #fff;
            outline: none;
            border-radius: 0px 16px 16px 0px;
            background-color: orange;
        }

        #show {
            width: 490px;
            position: absolute;
            left: 10px;
            top: 30px;
            border: 1px solid #ccc;
            border-top: none;
            display: none;
        }

        #show p {
            padding-left: 10px;
            line-height: 20px;
            color: orange;
            font-size: 13px;
            padding-right: 10px;
        }

        #show p a {
            text-decoration: none;
            float: right;
        }
    </style>
</head>
<body>
<div id="all">
    <input type="text" id="text">
    <button id="enter">淘宝搜索</button>
    <div id="show">

    </div>
</div>
<script src="js/jquery-1.8.3.min.js"></script>
<script>
    var text = $("#text");
    var enter = $("#enter");
    var show = $("#show");
    var data = localStorage.getItem("historyData") || "[]";
    var dataArr = JSON.parse(data);
    var init = function () {
        if (dataArr.length == 0){
            show.hide();
            return;
        }
        show.html("");
        $(dataArr).each(function (index, item) {
            $("<p></p>").text(item).prependTo(show).append($("<a href=&#39;javascript:;&#39;></a>").text("删除").attr("index", index));
        });
    }
    text.focus(function () {
        init();
        if(dataArr!=0)show.show();
    });
    enter.click(function () {
        var val = text.val().trim();
        if (val.length == 0) return;
        dataArr.push(val);
        localStorage.setItem("historyData", JSON.stringify(dataArr));
        text.val("");
        init();
    });
    $("#show").on("click", "a", function () {
        var index = $(this).attr("index");
        dataArr.splice(index, 1);
        localStorage.setItem("historyData", JSON.stringify(dataArr));
        init();
    });
</script>
</body>
</html>
登入後複製

最終效果圖:

什麼是html5的本機儲存功能

相關推薦:《html影片教學

以上是什麼是html5的本機儲存功能的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!