什麼是html5的本機儲存功能
html5
在html5中,本地儲存是一種讓網頁可以把鍵值對儲存在使用者瀏覽器客戶端的方法。透過本地存儲,web應用程式能夠在用戶瀏覽器中對資料進行本地的存儲。
本教學操作環境: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='javascript:;'></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>
登入後複製
最終效果圖:
相關推薦:《html影片教學》
以上是什麼是html5的本機儲存功能的詳細內容。更多資訊請關注PHP中文網其他相關文章!
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章
Windows 11 KB5054979中的新功能以及如何解決更新問題
3 週前
By DDD
如何修復KB5055523無法在Windows 11中安裝?
2 週前
By DDD
Inzoi:如何申請學校和大學
4 週前
By DDD
如何修復KB5055518無法在Windows 10中安裝?
2 週前
By DDD
Roblox:Dead Rails - 如何召喚和擊敗Nikola Tesla
1 個月前
By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)