zoukankan      html  css  js  c++  java
  • indexedDB简单例子

    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>HTML5 - IndexedDB</title>
    <script type="text/javascript">
    var db;
    var request;
    var objectStore;
    function _create(dbName){
        request=indexedDB.open(dbName,1);
        
        request.onerror = function () {
               alert("打开数据库失败:"+event.target.message);
        }
        request.onsuccess = function (event) {
            alert("打开数据库成功!");
               db=event.target.result;
               var transaction = db.transaction(["info"], "readwrite");
               objectStore = transaction.objectStore("info");
        } 
        request.onupgradeneeded = function(event) {
               alert("版本变化!");
               db = event.target.result;
               objectStore = db.createObjectStore("info", {keyPath: "userId",autoIncrement: true});
         }
    }
       
    function _delete(dbName){
        try{
               request=indexedDB.deleteDatabase(dbName);
               request.onerror = function () {
                   alert("删除数据库失败:"+event.target.message);
            }
            request.onsuccess = function (event) {
                alert("删除数据库成功!");
            } 
        }catch(e){
           alert(e.getMessage);
        }
    }
     
    function _get (argument) {
        var p=document.getElementById("display");
        p.innerHTML="";//获取数据前先清理一下页面已显示的数据
        if(!db){
            alert("请打开数据先!");
            return false;
        }
        var store = db.transaction("info").objectStore("info");
        var keyRange = IDBKeyRange.lowerBound(0);//规定keyRange从0开始
        var cursorRequest = store.openCursor(keyRange);//按照keyRange的设置开启游标
     
        cursorRequest.onsuccess = function (e) {
     
            var result = e.target.result;
     
            if (!!result == false)
                return;
     
            _render(result.value);
            result.continue();//这边执行轮询读取
        };
     
        cursorRequest.onerror = function (e) {
            alert("数据检索失败!");
        };
     
    }
     
    function _render (e) {
        var p=document.getElementById("display");
        p.innerHTML+="姓名:"+e.name+" 年龄:"+e.age+" 性别:"+e.xb+"<br />";
    }
     
    function _add (argument) {
        var name=document.getElementById("name").value;
        var age=document.getElementById("age").value;
        var xb,flag=document.getElementById("nan").checked;
        if(flag)
            xb="";
        else
            xb="";
        var detail={
            name:name,
            age:age,
            xb:xb
        }
        if(!db){
            alert("请打开数据先!");
            return false;
        }
           var transaction = db.transaction(["info"], "readwrite");
        objectStore = transaction.objectStore("info");
        objectStore.add(detail);
        alert("添加成功!");
    }
    </script>
    </head>
    <body>
        <small>添加前请先打开数据库(如数据库不存在则执行创建过程)</small><br /><br />
        <button onClick="_create('user')">打开数据库</button>
           <button onClick="_delete('user')">删除数据库</button><br /><br />
        姓名:<input type="text" id="name"><br />
        年龄:<input type="number" id="age" min=1><br />
        性别:<br />
              男:<input type="radio" id="nan" name="xb" value="male" checked>
              女:<input type="radio" id="nv" name="xb" value="female"><br />
        
        <button onClick="_add()">添加数据</button>
        <button onClick="_get()">获取数据</button><br />
        <p id="display"></p>
        
    </body>
    </html>
  • 相关阅读:
    从留言簿开始,学习MonoRail MVC(三)
    从留言簿开始,学习MonoRail MVC(二)
    程序集版本最后一位使用SVN版本号的自动生成方法
    如何让.Net控件在设计时InitializeComponent()中不生成相关代码
    [收藏]Web Services and C# Enums
    从留言簿开始,学习MonoRail MVC(一)
    .Net控制USB设备相关内容
    .Net 2.0ListView控件在Windows 2000和Windows XP上的差异
    基于高德地图Windows Phone API 快速开发地图相关APP(二)
    android map api v2 示例 步骤及问题
  • 原文地址:https://www.cnblogs.com/henuyuxiang/p/14690789.html
Copyright © 2011-2022 走看看