HTML5存储(带一个粗糙的打怪小游戏案例)

前端开发 作者: 2024-08-26 02:35:01
本地存储localStorage设置存储内容setItem(key,value) localStorage.setItem('leo','23'); 更新存储内容对象[
    localStorage.setItem('leo','23');
    localStorage.leo = 25;
    localStorage['leo'] = 24;
    console.log(localStorage.getItem('leo'))
    localStorage.removeItem('leo');
    localStorage.clear();
    console.log(localStorage.length);
    sessionStorage.a = 10;
    console.log(sessionStorage);



<!doctype html> 
<htmlhead> 
    meta charset="utf-8"title></style type="text/css">
*{margin: 0;paddinglist-style none}
bodyposition relativeheight 100%
html
.guai absoluteleft 50%top -75px 0 0 -100px
.linewidth 400px 20pxborder4px solid black 0 0 0 -204px
.xiebackground redtransition .3s}

    </style>
body>
    div class='line'>
        ='xie'divimg src="1.jpeg" class='guai'script ="text/javascript">
        var num = 0,timer null400= document.querySelector('.xie);

        if(localStorage.x){
            max  localStorage.x;
            xieNode.style.width  max + px;
        };

        onclick function(){
             r  Math.random() * 5 5;
            max -= r;

            localStorage.setItem(x;

            clearInterval(timer);
            timer  setInterval((){
                num++;
                (num == 10){
                    clearInterval(timer);
                    num ;
                    document.body.style.left ;
                    document.body.style.top ;
                    return;
                };
                document.body.style.left -20 10 ;
                document.body.style.top ;
            },30)
        }
    script>
>
    储存数据:
    input ="" name id='need'
    储存数据的时间:
    ='timer'button id='btn'>保存button
    数据展示:
    span ='span'>暂无数据span>

    >

         nowTime new Date().getMinutes();

        (nowTime >= localStorage.timer){
            localStorage.clear();
        }
        else{
            (localStorage.leo){
                span.innerHTML  localStorage.leo;
            }

        }

        btn.onclick (){
            localStorage.setItem(leotimer Date().getMinutes() + Number(timer.value));
            span.innerHTML  localStorage.leo;
        };
    >
    var request = indexedDB.open('testDBLeo',6);
    // 数据库创建成功
    request.onsuccess = function(){
        console.log('创建数据库成功');
    };
     数据库创建失败
    request.onerror = (){
        console.log('数据库读取失败' 数据库版本升级
    request.onupgradeneeded = (){
        console.log('版本号升级了')
    };
    request.onupgradeneeded = (){
        var db = request.result;
         一个ObjectStore相当于一张表
         指定表的主键自增
        db.createObjectStore('test3',{autoIncrement: true});
    };
    db.createObjectStore('test3',{keyPath: 'id'}
    store.createIndex('test3','name',{unique:true});  
        var transaction = db.transaction('test3','readwrite');
        var store = transaction.objectStore('test3');
        var json = [{
            "id":200
        },{
            "id":201
        }]
         add 添加数据
        store.add(json);    
         读取数据store.get()参数是主键的值
        var requestNode = store.get(1获取成功后的操作
        requestNode.onsuccess = (){
            console.log(requestNode.result);
            for(var i=0;i<3;i++){
                console.log('名字叫'+requestNode.result[i].name);
                console.log('年龄今年已经'+requestNode.result[i].age+'岁了');
            }
        };
    store.put({
        "id":203
    });
    var requestNode = store.getAll();
    store.delete(201);
    var requestNode = store.openCursor(IDBKeyRange.upperBound(202));
    requestNode.onsuccess = 获取游标所取得的值
        var cursor = requestNode.result;
        if(cursor){
            console.log(cursor.value);
            cursor.continue();
        };  
    };
    store.createIndex(表名称,数据key值,1)">});
----------
    var index = store.index(表的名称)get(key值的名称).onsuccess = (){
        e.target.result 找到的数据的内容
    }
    var requestNode = store.openCursor(IDBKeyRange.bound(200,202),'prev');
       指定数据表
        var index = store.index('test3'游标指定范围
        var requestNode = index.openCursor(IDBKeyRange.upperBound(31));

        requestNode.onsuccess = (){
             requestNode.result;
            (cursor){
                如果查询的数据name为Leo
                if(cursor.value.name == 'Leo'){
                     更新数据
                    cursor.update({
                        "id":209
                    });
                }
                console.log(cursor.value);
                cursor.();
            }
        };
原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_68732.html