HTML5+ 學習筆記3 storage.增刪改查


//插入N條數據

function
setItemFun( id ) { //循環插入100調數據 var dataNum = new Number(id); for ( var i=0; i<dataNum; i++ ) { plus.storage.setItem( "key"+i, "value"+i ); } var length = getLengthFun(); outSet( "數據存儲成功,存儲了"+length+"條數據;" ); } function getLengthFun(){ return plus.storage.getLength(); }

//查詢全部數據
function getAllItem(){ outSet( "獲取數據:" ); var itemLength = getLengthFun(); for ( var i=0; i < itemLength; i++ ) { var key = plus.storage.key(i); var value = plus.storage.getItem(key); outLine( "key:"+key+"-->value:"+value ); }; }

//刪除部分Item
function delItem() { outSet( "刪除數據:" ); var itemLength = getLengthFun(); for (var i=0; i < itemLength; i+=2) { var key = plus.storage.key(i); plus.storage.removeItem(key); }; var itemNo = getLengthFun(); outLine( "移除了"+(itemLength-itemNo)+"條數據" ); }

//清除全部內容
function clearAllItem(){ plus.storage.clear(); var num = getLengthFun(); if ( num == 0 ) { outSet( "storage數據清除成功!" ); }else{ outSet( "storage數據清除失敗!" ); } }

//修改前五條數據的內容
function modifyTopFive(){ outSet( "修改前的數據是:" ); for (var i=0; i < 5; i++) { var key = plus.storage.key(i),value = plus.storage.getItem(key); outLine( "key:"+key+"-->value:"+value ); plus.storage.setItem( key, "新數據值>>"+i ); }; outLine( "修改後的數據是:" ); for (var i=0; i < 5; i++) { var key = plus.storage.key(i),value = plus.storage.getItem(key); outLine( "key:"+key+"-->value:"+value ); }; }
相關文章
相關標籤/搜索