在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中文網其他相關文章!

useState()isaReacthookusedtomanagestateinfunctionalcomponents.1)Itinitializesandupdatesstate,2)shouldbecalledatthetoplevelofcomponents,3)canleadto'stalestate'ifnotusedcorrectly,and4)performancecanbeoptimizedusinguseCallbackandproperstateupdates.

ReactispupularduetoItsOmpontement,基於虛擬,虛擬詞,Richecosystem和declarativedation.1)基於組件的harchitectureallowslowsforreusableuipieces。

todebugreactapplicationsefectefectionfection,usethestertate:1)proppropdrillingwithcontextapiorredux.2)使用babortControllerToptopRollerTopRollerTopRollerTopRollerTopRollerTopRollerTopRollerTopRollerTopRollerTopRaceeDitions.3)intleleassynChronOusOperations.3)

usestate()inrectallowsStateMagementionInfunctionalComponents.1)ITSIMPLIFIESSTATEMAGEMENT,MACHECODEMORECONCONCISE.2)usetheprevcountfunctionToupdateStateBasedonitspReviousViousViousvalue,deveingingStaleStateissues.3)

selectUsestate()forsimple,獨立的variables; useusereducer()forcomplexstateLogicorWhenStatedIppedsonPreviousState.1)usestate()isidealForsImpleupDatesLikeToggGlikGlingaBglingAboolAboolAupDatingacount.2

useState優於類組件和其它狀態管理方案,因為它簡化了狀態管理,使代碼更清晰、更易讀,並與React的聲明性本質一致。 1)useState允許在函數組件中直接聲明狀態變量,2)它通過鉤子機制在重新渲染間記住狀態,3)使用useState可以利用React的優化如備忘錄化,提升性能,4)但需注意只能在組件頂層或自定義鉤子中調用,避免在循環、條件或嵌套函數中使用。

useUsestate()forlocalComponentStateMangementighatighation; 1)usestate()isidealforsimple,localforsimple.2)useglobalstate.2)useglobalstateSolutionsLikErcontExtforsharedState.3)

ReusableComponentsInrectenHanceCodainainability and效率byallowingDevelostEsteSeTheseTheseThesAmeCompOntionActActRossDifferentPartSofanApplicationorprojects.1)heSredunceRedUndenceNandSimplifyUpdates.2)yensureconsistencyInuserexperience.3)


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

Dreamweaver Mac版
視覺化網頁開發工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

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

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

Dreamweaver CS6
視覺化網頁開發工具