2012-10-31 117 views
2

我想存儲計數器的結果並將信息返回到另一頁上。html5 localstorage存儲和訪問

到目前爲止,我這個..

<script> 

function clickCounter() { 
document.getElementById("p4").style.display="none"; 

if(typeof(Storage)!=="undefined") { 

if (localStorage.clickcount) { 
localStorage.clickcount=Number(localStorage.clickcount)+1;} 

else { 
localStorage.clickcount=1;} 
document.getElementById("result").innerHTML="You have had " + localStorage.clickcount + " drink(s)";} 

else { 
document.getElementById("result").innerHTML="Sorry, your browser does not support web storage.";} 
} 
</script> 

所以櫃檯的工作,當我按下按鈕,它增加按1。

我不知道我在哪裏堅持在.setItem("result");然後是.getItem("result")方法。

有點無能,任何幫助都會很棒!謝謝!

回答

1

您需要使用setItem()和getItem()方法來訪問localStorage,而不是直接針對「localStorage」對象獲取/設置屬性。這裏是我的意思的例子:

//Saving a new item named "foo" with a value of "bar" 
window.sessionStorage.setItem("foo", "bar"); 

//Retrieveing a saved item: 
var value = window.localStorage.getItem("foo"); //returns "bar" 

//Deleting an item 
localStorage.removeItem("foo"); 

//Clearing all local storage 
localStorage.clear(); 

下面是我創建的演示如何從localStorage的添加,刪除和明確項目的範例頁面:

http://blackberry.github.com/WebWorks-Samples/kitchenSink/html/html5/storage.html