简体   繁体   English

未捕获的TypeError:无法将innerHTML设置为null

[英]Uncaught TypeError: Cannot set innerHTML to null

Can somebody tell why I get this innerHTML error in the following code? 有人可以在下面的代码中告诉我为什么收到此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>

I'm following this tutorial: http://www.html5rocks.com/en/tutorials/webdatabase/todo/ 我正在关注本教程: http : //www.html5rocks.com/en/tutorials/webdatabase/todo/

What happens if you add the todoItems element in the body along with the form from the article? 如果添加会发生什么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.

相关问题 未捕获的TypeError:无法将属性&#39;innerHTML&#39;设置为null - Uncaught TypeError: Cannot set property 'innerHTML' of null 未捕获的Typeerror:无法将属性innerHTML设置为null - Uncaught Typeerror: Cannot set property innerHTML of null 未捕获的TypeError:无法将属性&#39;innerHTML&#39;设置为null - Uncaught TypeError: Cannot set property 'innerHTML' of null 未捕获的TypeError:无法将属性&#39;innerHTML&#39;设置为null - Uncaught TypeError: Cannot set property 'innerHTML' of null 未捕获的TypeError:无法设置null的属性&#39;innerHTML&#39; - Uncaught TypeError: Cannot set property 'innerHTML' of null 未捕获的TypeError:无法在phonegap中将属性&#39;innerHTML&#39;设置为null - Uncaught TypeError: Cannot set property 'innerHTML' of null in phonegap 未捕获的TypeError:无法使用for循环将属性&#39;innerHTML&#39;设置为null - Uncaught TypeError: Cannot set property 'innerHTML' of null using a for loop 未捕获的TypeError:无法在第34行中将属性&#39;innerHTML&#39;设置为null - Uncaught TypeError: Cannot set property 'innerHTML' of null in line 34 Uncaught TypeError:无法在JS创建的div上将属性&#39;innerHTML&#39;设置为null - Uncaught TypeError: Cannot set property 'innerHTML' of null on JS created div 未被捕获的TypeError:无法将属性&#39;innerHTML&#39;设置为null [VueJs] - Uncaught TypeError: Cannot set property 'innerHTML' of null [VueJs]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM