我有一个 AJAX 应用程序,它会下载一个 JSON 对象,并使用 Javascript DOM 函数将数据添加到 HTML <table> 中的行中。在其他浏览器上都能完美运行,但在 Internet Explorer 上却不行。IE 不会给出任何错误信息,而我已经尽力验证代码是否被浏览器执行,但它似乎没有任何效果。我创建了这个简单粗暴的页面来演示这个问题:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><title></title></head><body>
<table id="employeetable">
<tr>
<th>Name</th>
<th>Job</th>
</tr>
</table>
<script type="text/javascript">
function addEmployee(employeeName, employeeJob) {
var tableElement = document.getElementById("employeetable");
if (tableElement) {
var newRow = document.createElement("tr");
var nameCell = document.createElement("td");
var jobCell = document.createElement("td");
nameCell.appendChild(document.createTextNode(employeeName));
jobCell.appendChild(document.createTextNode(employeeJob));
newRow.appendChild(nameCell);
newRow.appendChild(jobCell);
tableElement.appendChild(newRow);
alert("code executed!");
}
}
setTimeout("addEmployee(\"Bob Smith\", \"CEO\");", 1000);
setTimeout("addEmployee(\"John Franks\", \"Vice President\");", 2000);
setTimeout("addEmployee(\"Jane Doe\", \"Director of Marketing\");", 3000);
</script>
</body></html>
我还没有尝试过IE 8,但是IE7和IE6都没有显示额外的行,这些行据说已经被添加了。我无法理解为什么会这样。是否有人知道解决这个问题的好方法,或者我可能做错了什么?
tableElement.getElementsByType('tbody')
应该返回这里期望的内容。 - Tetsujin no OnitableElement.getElementsByTagName('tbody')[0]
。 - Travesty3<tbody>
,但在IE上仍然无法正常工作,我的问题。 - user801116