有人可以告訴我爲什麼在下面的代碼中得到這個innerHTML錯誤嗎?未捕獲的類型錯誤:無法將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/
在哪條線上得到錯誤?您是否已經在開發人員工具中執行了執行? – robertc
是的,我做到了。它說錯誤在這一行:todoItems.innerHTML = rowOutput; –
你可以嘗試'todoItems.innerHTML = rowOutput || '''如果有可能rowOutput可能沒有被正確填充,它會給你一個佔位符。 –