正在回答 回答被采纳积分+1
3回答
hunmix
2018-03-09 20:08:51
//data var data=[{ "name":"史莱姆", "id":"001", "hp":100 },{ "name":"波哥布林", "id":"002", "hp":150 },{ "name":"哥莫布林", "id":"003", "hp":200 }] var dbName="mine", osName="os1", version=2; var db = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB; //关闭数据库 function closeDB(db){ db.close(); } console.log(db) //删除数据库 function deleteDB(name){ var request=db.deleteDatabase(name); request.onsuccess=function(){ console.log('deleteDB success') } } function openDB(DBname,version){ var request=db.open(DBname,version); //更新数据库版本 request.onupgradeneeded=function(){ db=request.result; version=version || 1; if(!db.objectStoreNames.contains(osName)){ //添加表 设置键值类型 db.createObjectStore(osName,{keyPath:'id'}); } console.log('DBversion change to '+ version); } //请求失败 request.onerror=function(e){ console.log(e.currentTarget.error.message); } //请求成功 request.onsuccess=function(){ db=request.result; console.log('open success') } } openDB(dbName,version); // use IDBObjectStore.add to save data function addData(name){ //设置事务模式(read,readwrite,versionchange) var transaction = db.transaction(osName, 'readwrite'); //获取os1 对象 var store = transaction.objectStore(osName); //遍历data data.map(function(obj){ store.add(obj); }) } function getData(id){ var transaction = db.transaction(osName,'readwrite'), store=transaction.objectStore(osName), request=store.get(id); request.onsuccess=function(){ console.log(request.result); } } function getAll(){ var transaction=db.transaction(osName,'readwrite'), store=transaction.objectStore(osName), request=store.getAll(); request.onsuccess=function(){ console.log(request.result); } } function updateData(id){ var transaction=db.transaction(osName,'readwrite'), store=transaction.objectStore(osName), request=store.get(id); request.onsuccess=function(){ store.put({ "name":"林克小可爱", "id":id, "hp":"max" }); console.log("update success") } } function deleteData(id){ var transaction=db.transaction(osName,'readwrite'), store=transaction.objectStore(osName); var request=store.delete(id); request.onsuccess=function(){ console.log('delete success'); } } function clearData(){ var transaction=db.transaction(osName,'readwrite'), store=transaction.objectStore(osName); request=store.clear(); request.onsuccess=function(){ console.log('clear success'); } }
这是代码。。。现在还是loading
组件化思想开发电商网页 18版
- 参与学习 人
- 提交作业 467 份
- 解答问题 4826 个
本路径带你通过系统学习HTML5、JavaScript、jQuery的进阶知识,不仅如此,还会学习如何利用组件化的思想来开发网页,知识点+案例,使得所学可以更好的得到实践。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星