○ getItem(key) → 回傳index對應的資料 (data)
○ setItem(key,data) → 儲存index對應的資料 (void)
○ removeItem(key) → 刪除key對應的資料 (void)
○ clear() → 刪除所有的資料 (void)
=============================================================================================
※ 練習控制方法
<HTML>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="ccsName.css">
<script src="8_new.js"></script>
<title>Storage Access</title>
</head>
<body>
Key:<input type="text" id="keyText"> <p>
Value:<input type="text" id="valueText"> <p>
<hr width="40%" align="left">
<button id="setButton" onclick="storageAccess(1)">Set Button</button>
<button id="getButton" onclick="storageAccess(0)">Get Button</button>
<button id="removeButton" onclick="storageAccess(2)">Remove Button</button>
<button id="clearButton" onclick="storageAccess(3)">Clear Button</button>
</body>
</html>
-------------------------------------------------------------------------------------------------------------------------------
<JavaScript>
function storageAccess(id){
var storage = localStorage;
var key = document.getElementById('keyText').value; //跟畫面產生關聯
var value = document.getElementById('valueText').value;
switch(id){ // switch單一敘述
case 0:
var item = storage.getItem(key);
alert(item);
break;
case 1:
storage.setItem(key,value);
alert(key+'/'+value+':setItemFinish');
break;
case 2:
storage.removeItem(key,value);
alert(key+'/'+value+':removeItemFinish');
break;
case 3:
storage.clear();
alert('Clear All');
break;
default:
break;
}// end of switch
}
沒有留言:
張貼留言