Cordova Storage使用

1.設置html按鈕標籤

<button id="set">設置storage</button>
<button id='get'>獲取storage</button>
<button id='remove'>刪除單個storage</button>
<button id='getByKey'>索引獲取storage</button>
<button id='length'>獲取長度</button>
<button id="clear">清空storage</button>

2. localStorage.setItem(key,value) 存儲storage

document.getElementById('set').onclick=function(){
    localStorage.setItem('name','xxx');
    localStorage.setItem('age',18);
}

3.localStorage.getItem(key) 讀取storage

document.getElementById('get').onclick=function(){
    alert(localStorage.getItem('name')+localStorage.getItem('age'));
}

4.localstorage.removeItem(key) 根據key刪除storage

document.getElementById('remove').onclick=function(){
    // 指定單個刪除
    localStorage.removeItem('name');
}

5.localStorage.key(index) 獲取索引的key值

document.getElementById('getByKey').onclick=function(){
    // key是根據字母順序的 字母越小排越前
    alert(localStorage.key(0))
}

6. localstorage.length 獲取

document.getElementById('length').onclick=function(){
    alert(localStorage.length);
}

7.localstorage.clear() 清空storage

document.getElementById('clear').onclick=function(){
    // 全部刪除
    localStorage.clear();
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章