未捕获的类型错误:无法将 innerHTML 设置为 null

发布于 2024-12-05 22:49:52 字数 3120 浏览 2 评论 0原文

有人能告诉我为什么我在下面的代码中收到这个innerHTML错误吗?

<html>
<head>
    <title>
        My Todo List
    </title>
    </head>
    <script type="text/javascript">
        var html5rocks = {};
        html5rocks.webdb = {};

        html5rocks.webdb.db = null;

        html5rocks.webdb.open = function() {
          var dbSize = 5 * 1024 * 1024; // 5MB
          html5rocks.webdb.db = openDatabase('Todo', '1.0', 'todo manager', dbSize);
        }

        html5rocks.webdb.onError = function(tx, e) {
          alert('Something unexpected happened: ' + e.message );
        }

        html5rocks.webdb.onSuccess = function(tx, r) {
          // re-render all the data
          // loadTodoItems is defined in Step 4a
          html5rocks.webdb.getAllTodoItems(loadTodoItems);
          }
        html5rocks.webdb.createTable = function() {
        html5rocks.webdb.db.transaction(function(tx) {
            tx.executeSql('CREATE TABLE IF NOT EXISTS ' + 
                          'todo(ID INTEGER PRIMARY KEY ASC, todo TEXT, added_on DATETIME)', []);
          });
        }

        html5rocks.webdb.addTodo = function(todoText) {
          html5rocks.webdb.db.transaction(function(tx){
            var addedOn = new Date();
            tx.executeSql('INSERT INTO todo(todo, added_on) VALUES (?,?)', 
                [todoText, addedOn],
                html5rocks.webdb.onSuccess,
                html5rocks.webdb.onError);
            });
        }
        html5rocks.webdb.getAllTodoItems = function(renderFunc) {
          html5rocks.webdb.db.transaction(function(tx) {
            tx.executeSql('SELECT * FROM todo', [], renderFunc, 
                html5rocks.webdb.onError);
          });
        }
        function loadTodoItems(tx, rs) {
          var rowOutput = "";
          for (var i=0; i < rs.rows.length; i++) {
            rowOutput += renderTodo(rs.rows.item(i));
          }
          var todoItems = document.getElementById('todoItems');
          todoItems.innerHTML = rowOutput;
        }

        function renderTodo(row) {
          return '<li>' + row.ID  +
                 '[<a onclick="html5rocks.webdb.deleteTodo(' + row.ID + ');">X</a>]</li>';
        }

        html5rocks.webdb.deleteTodo = function(id) {
          html5rocks.webdb.db.transaction(function(tx) {
            tx.executeSql('DELETE FROM todo WHERE ID=?', [id],
            html5rocks.webdb.onSuccess, html5rocks.webdb.onError);
            });
        }
        function addTodo() {
          var todo = document.getElementById('todo');
          html5rocks.webdb.addTodo(todo.value);
          todo.value = '';
        }
        function init() {
          html5rocks.webdb.open();
          html5rocks.webdb.createTable();
          html5rocks.webdb.getAllTodoItems(loadTodoItems);
        }
    </script>
    </head>
<body onload="init()">
</body>

我正在关注本教程: http://www.html5rocks.com/en/tutorials /webdatabase/todo/

Can somebody tell why I get this innerHTML error in the following code?

<html>
<head>
    <title>
        My Todo List
    </title>
    </head>
    <script type="text/javascript">
        var html5rocks = {};
        html5rocks.webdb = {};

        html5rocks.webdb.db = null;

        html5rocks.webdb.open = function() {
          var dbSize = 5 * 1024 * 1024; // 5MB
          html5rocks.webdb.db = openDatabase('Todo', '1.0', 'todo manager', dbSize);
        }

        html5rocks.webdb.onError = function(tx, e) {
          alert('Something unexpected happened: ' + e.message );
        }

        html5rocks.webdb.onSuccess = function(tx, r) {
          // re-render all the data
          // loadTodoItems is defined in Step 4a
          html5rocks.webdb.getAllTodoItems(loadTodoItems);
          }
        html5rocks.webdb.createTable = function() {
        html5rocks.webdb.db.transaction(function(tx) {
            tx.executeSql('CREATE TABLE IF NOT EXISTS ' + 
                          'todo(ID INTEGER PRIMARY KEY ASC, todo TEXT, added_on DATETIME)', []);
          });
        }

        html5rocks.webdb.addTodo = function(todoText) {
          html5rocks.webdb.db.transaction(function(tx){
            var addedOn = new Date();
            tx.executeSql('INSERT INTO todo(todo, added_on) VALUES (?,?)', 
                [todoText, addedOn],
                html5rocks.webdb.onSuccess,
                html5rocks.webdb.onError);
            });
        }
        html5rocks.webdb.getAllTodoItems = function(renderFunc) {
          html5rocks.webdb.db.transaction(function(tx) {
            tx.executeSql('SELECT * FROM todo', [], renderFunc, 
                html5rocks.webdb.onError);
          });
        }
        function loadTodoItems(tx, rs) {
          var rowOutput = "";
          for (var i=0; i < rs.rows.length; i++) {
            rowOutput += renderTodo(rs.rows.item(i));
          }
          var todoItems = document.getElementById('todoItems');
          todoItems.innerHTML = rowOutput;
        }

        function renderTodo(row) {
          return '<li>' + row.ID  +
                 '[<a onclick="html5rocks.webdb.deleteTodo(' + row.ID + ');">X</a>]</li>';
        }

        html5rocks.webdb.deleteTodo = function(id) {
          html5rocks.webdb.db.transaction(function(tx) {
            tx.executeSql('DELETE FROM todo WHERE ID=?', [id],
            html5rocks.webdb.onSuccess, html5rocks.webdb.onError);
            });
        }
        function addTodo() {
          var todo = document.getElementById('todo');
          html5rocks.webdb.addTodo(todo.value);
          todo.value = '';
        }
        function init() {
          html5rocks.webdb.open();
          html5rocks.webdb.createTable();
          html5rocks.webdb.getAllTodoItems(loadTodoItems);
        }
    </script>
    </head>
<body onload="init()">
</body>

I'm following this tutorial: http://www.html5rocks.com/en/tutorials/webdatabase/todo/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

禾厶谷欠 2024-12-12 22:49:52

如果将 todoItems 元素与文章中的表单一起添加到 body 中,会发生什么?

<body onload="init()">
    <ul id="todoItems"></ul>
    <form type="post" onsubmit="addTodo(); return false;">
        <input type="text" id="todo" name="todo" placeholder="What do you need to do?" style="width: 200px;">
        <input type="submit" value="Add Todo Item">
    </form>
</body>

What happens if you add the todoItems element in the body along with the form from the article?

<body onload="init()">
    <ul id="todoItems"></ul>
    <form type="post" onsubmit="addTodo(); return false;">
        <input type="text" id="todo" name="todo" placeholder="What do you need to do?" style="width: 200px;">
        <input type="submit" value="Add Todo Item">
    </form>
</body>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文