加载窗口时出现错误= javascript的innerHTML null

时间:2019-06-21 19:15:26

标签: javascript html web-sql

为什么会出现错误或

  

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

每次加载窗口?

我试图显示来自Web sql的数据

顺便说一句,我将javascript放在了不同的文件(main.js

JavaScript函数:

if (window.openDatabase) 
{
    var mydb = openDatabase("data", "0.1", "personal data", 1024 * 1024);

    mydb.transaction(function (t) 
    {
        t.executeSql("CREATE TABLE IF NOT EXISTS person (id INTEGER PRIMARY KEY ASC, gender TEXT, weight INTEGER");
    });
} 
else 
{
    alert("Browser doesnt support web sql!");
}

function show_data() {
    if (mydb) {
        mydb.transaction(function (t) {
            t.executeSql("SELECT * FROM person", [], update_list);
        });
    } else {
        alert("database not found, browser doesnt support web sql!");
    }
}

show_data();

function update_list(transaction, results) 
{

    var listholder = document.getElementById("list_data");

    listholder.innerHTML="";

    var i;
    for (i = 0; i < results.rows.length; i++) 
    {

        var row = results.rows.item(i);

        listholder.innerHTML += 
        `<tr>
        <td>${row.gender}</td>
        <td>${row.weight}</td>  
        </tr>`;
    }
}
<body>
<table border="0" cellpadding="5" cellspacing="5">
  <thead>
    <td><div class="list-item__left">Gender</div></td>
    <td><div class="list-item__left">Weight</div></td>
  </thead>
  <tbody  id="list_data">
  </tbody>
</table>
<script src="js/main.js"></script>
</body>

谢谢

0 个答案:

没有答案