1)使用本機儲存(localStorage):
透過全域屬性localStorage存取本機儲存功能,並會傳回一個Storage物件,它被用來保存鍵/值形式的字串對。
Storage物件的成員:
# clear()-移除已儲存的鍵/值對;
getItem(b426c87d5d3d3a1fd68262c64998cac2)-取得指定鍵相關的值;
key(< ;index>)-取得指定索引的鍵;
length-傳回已儲存的鍵/值對數量;
## removeItem(42538adbdb6240b2b083a000a615d5bd)-移除指定鍵對應的鍵/值對;
set#Item(42538adbdb6240b2b083a000a615d5bd ,8487820b627113dd990f63dd2ef215f3)-新增一個新的鍵/值對,如果鍵已使用就更新它的值;
[42538adbdb6240b2b083a000a615d5bd ]-以陣列的存取形式取得與指定鍵關聯的值;
# 監聽儲存事件:
# 某個文件修改本機儲存時會觸發storage事件,同時指派的對象為StorageEvent對象,其成員有:
key-回傳發生變更的鍵;
oldValue-傳回關聯此鍵的舊值;
newValue-傳回關聯此鍵的新值;
url-傳回製造變更的文件URL;
storageArea-回傳發生變更的Storage物件;
#
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>上海远地资产管理有限公司</title> <meta name="author" content="jason"/> <meta name="description" content="上海远地资产管理有限公司(简称:远地资产),是一家专业的互联网金融服务平台."/> <link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon"/> <style type="text/css"> body > *{ float:left; } table{ border-collapse: collapse; margin-left: 50px; } th,td{ padding: 4px; } th{ text-align: right; } input{ border:thin solid black; padding: 2px; } label{ min-width: 50px; display: inline-block; text-align: right; } #countmsg,#buttons{ margin-left: 50px; margin-top:5px; margin-bottom: 5px; } </style> </head> <body> <p> <p> <label>Key:</label> <input id="key" placeholder="Enter Key"> </p> <p> <label>Value:</label> <input id="value" placeholder="Enter Value"> </p> <p id="buttons"> <button id="add">Add</button> <button id="clear">Clear</button> </p> <p id="countmsg">There are <span id="count"></span> items</p> </p> <table id="data" border="1"> <tr> <th>Item Count:</th><td>-</td> </tr> </table> <script> displayData(); var buttons=document.getElementsByTagName("button"); for(var i=0;i<buttons.length;i++){ buttons[i].onclick=handleButtonPress; } function handleButtonPress(e){ switch(e.target.id){ case 'add': var key=document.getElementById("key").value; var value=document.getElementById("value").value; localStorage.setItem(key,value); break; case 'clear': localStorage.clear(); break; } displayData(); } function displayData(){ var tableElem=document.getElementById("data"); tableElem.innerHTML=""; var itemCount=localStorage.length; document.getElementById("count").innerHTML=itemCount; for(var i=0;i<itemCount;i++){ var key=localStorage.key(i); var val=localStorage[key]; tableElem.innerHTML+="<tr><th>"+key+":</th><td>"+val+"</td></tr>"; } } </script> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> table{ border-collapse: collapse; margin-left: 50px; } th,td{ padding: 4px; } </style> </head> <body> <table id="data" border="1"> <tr> <th>key</th> <th>oldValue</th> <th>newValue</th> <th>url</th> <th>storageArea</th> </tr> </table> <script> var tableElem=document.getElementById("data"); window.onstorage=handleStorage; function handleStorage(e){ var row="<tr>"; row+="<td>"+ e.key+"</td>"; row+="<td>"+ e.oldValue+"</td>"; row+="<td>"+ e.newValue+"</td>"; row+="<td>"+ e.url+"</td>"; row+="<td>"+ (e.storageArea == localStorage)+"</td></tr>"; tableElem.innerHTML+=row; } </script> </body> </html>
2)使用會話儲存(sessionStorage)
會話儲存的工作方式和本機儲存很接近,不同之處在於資料是各個瀏覽器上下文私有的,會在文件關閉時移除。
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>上海远地资产管理有限公司</title> <meta name="author" content="jason"/> <meta name="description" content="上海远地资产管理有限公司(简称:远地资产),是一家专业的互联网金融服务平台."/> <link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon"/> <style type="text/css"> body > *{ float:left; } table{ border-collapse: collapse; margin-left: 50px; } th,td{ padding: 4px; } th{ text-align: right; } input{ border:thin solid black; padding: 2px; } label{ min-width: 50px; display: inline-block; text-align: right; } #countmsg,#buttons { margin-left: 50px; margin-top: 5px; margin-bottom: 5px; } </style> </head> <body> <p> <p> <label>Key:</label> <input id="key" placeholder="Enter Key"> </p> <p> <label>Value:</label> <input id="value" placeholder="Enter Value"> </p> <p id="buttons"> <button id="add">Add</button> <button id="clear">Clear</button> </p> <p id="countmsg">There are <span id="count"></span> items</p> </p> <table id="data" border="1"> <tr> <th>Item Count:</th><td>-</td> </tr> </table> <script> displayData(); var buttons=document.getElementsByTagName("button"); for(var i=0;i<buttons.length;i++){ buttons[i].onclick=handleButtonPress; } function handleButtonPress(e){ switch(e.target.id){ case 'add': var key=document.getElementById("key").value; var value=document.getElementById("value").value; sessionStorage.setItem(key,value); break; case 'clear': sessionStorage.clear(); break; } displayData(); } function displayData(){ var tableElem=document.getElementById("data"); tableElem.innerHTML=""; var itemCount=sessionStorage.length; document.getElementById("count").innerHTML=itemCount; for(var i=0;i<itemCount;i++){ var key=sessionStorage.key(i); var val=sessionStorage[key]; tableElem.innerHTML+="<tr><th>"+key+":</th><td>"+val+"</td></tr>"; } } </script> </body> </html>
以上是html5之使用web儲存的具體介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!