簡體   English   中英

未捕獲的TypeError:無法將innerHTML設置為null

[英]Uncaught TypeError: Cannot set innerHTML to null

有人可以在下面的代碼中告訴我為什么收到此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/

如果添加會發生什么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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM