代码
提交代码
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>离线记事本</title> <meta name="viewport" content="width=device-width,initial-scale=1"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" /> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script><!-- 引用jQuery插件 --> </head> <script> var datatable = null; var db = openDatabase("note", "", "notebook", 1024 * 100); //初始化函数方法 function init() { datatable = document.getElementById("datatable"); showAllData(); } function removeAllData() { for(var i = datatable.childNodes.length - 1; i >= 0; i--) { datatable.removeChild(datatable.childNodes[i]); } var tr = document.createElement("tr"); var th1 = document.createElement("th"); var th2 = document.createElement("th"); var th3 = document.createElement("th"); th1.innerHTML = "标题"; th2.innerHTML = "内容"; th3.innerHTML = "时间"; tr.appendChild(th1); tr.appendChild(th2); tr.appendChild(th3); datatable.appendChild(tr); } //显示数据库中的数据 function showData(row) { var tr = document.createElement("tr"); var td1 = document.createElement("td"); td1.innerHTML = row.title; var td2 = document.createElement("td"); td2.innerHTML = row.content; var td3 = document.createElement("td"); var t = new Date(); t.setTime(row.time); td3.innerHTML = t.toLocaleDateString() + " " + t.toLocaleTimeString(); tr.appendChild(td1); tr.appendChild(td2); tr.appendChild(td3); datatable.appendChild(tr); } //显示所有的数据 function showAllData() { db.transaction(function(tx) { tx.executeSql("CREATE TABLE IF NOT EXISTS item(title TEXT,content TEXT,time INTEGER)", []); tx.executeSql("SELECT * FROM item", [], function(tx, rs) { removeAllData(); for(var i = 0; i < rs.rows.length; i++) { showData(rs.rows.item(i)) } }) }) } //添加一条记事本数据 function addData(title, content, time) { db.transaction(function(tx) { tx.executeSql("INSERT INTO item VALUES (?,?,?)", [title, content, time], function(tx, rs) { alert("保存成功!"); }, function(tx, error) { alert(error.source + "::" + error.message); } ) }) } //点击保存按钮 function saveData() { var title = document.getElementById("name").value; var content = document.getElementById("memo").value; var time = new Date().getTime(); addData(title, content, time); showAllData(); } </script> <body onload="init()"> <div data-role="page" id="pageone"> <div data-role="header" data-position="fixed"> <h1>离线记事本</h1> </div> <div data-role="main" class="ui-content"> <p align="center">记事</p> <table data-role="table" class="ui-responsive"> <thead> <tr> <th>标题:</th> <th>内容:</th> </tr> </thead> <tbody> <tr> <td><input type="text" id="name"></td> <td><input type="text" id="memo"></td> </tr> </tbody> </table> <button type="submit" onclick="saveData()">保存</button> <table data-role="table" data-mode="" class="ui-responsive" id="datatable"> </table> </div> </div> </body> </html>
运行结果