使用window.onload = function(){}仍然无法解决“按钮”
程序代码:
<!DOCTYPE HTML > <HTML> <HEAD> <TITLE> 实验一下 </TITLE> </HEAD> <BODY> <H2>员工信息列表</H2> <input type = "button" value = "显示员工信息列表" id = "displayBtn "/> <hr> <table border = 1px width = "50%"> <tr> <th>员工编号</th> <th>员工名字</th> <th>员工薪资</th> </tr> <TBODY id = "emptbody"> <!-- <tr> <td>7369</td> <td>SMITH</td> <td>800</td> </tr> <tr> <td>7369</td> <td>SMITH</td> <td>800</td> </tr> <tr> <td>7369</td> <td>SMITH</td> <td>800</td> </tr> --> </TBODY> </table> 总共<span id = "count">0</span>条数 <script type = "text/javascript"> //有这些json数据 var data = { "total" :4, "emps" : [ {"empno" : 7369, "ename" : "SMITH" , "sal":800.0}, {"empno" : 7361, "ename" : "SMITH2" , "sal":1800.0}, {"empno" : 7362, "ename" : "SMITH3" , "sal":2808.0}, {"empno" : 7363, "ename" : "SMITH4" , "sal":3800.0} ] } //希望把数据展示到table中 window.onload = function(){ //alert(11); document.getElementById("displayBtn").onclick = function(){ var emps = data.emps; var html = ""; for(var i = 0; i <emps.length; i++){ var emp = emps[i]; html += "<tr>"; html += "<td>"+emp.empno+"</td>"; html += "<td>"+emp.ename+"</td>"; html += "<td>"+emp.sal+"</td>"; html += "</tr>"; } document.getElementById("emptbody").innerHTML = html; document.getElementById("count").innerHTML = data.total; } } </script> </BODY> </HTML>
按下按钮“显示员工信息列表”却不能将详细信息显示出来,控制台显示“Uncaught TypeError: Cannot set property 'onclick' of null
at window.onload”,我有使用window.onload啊,也有将onclick放到下面来。