我有一個AJAX應用程序,它下載一個JSON對象並使用這些數據將行添加到使用Javascript DOM函數的HTML <表>。它完美的工作...除了在Internet Explorer中。 IE並沒有給出任何錯誤,並且我儘可能地驗證了代碼正在被瀏覽器執行,但它沒有任何效果。我創造了這個快速和骯髒的網頁來演示該問題:無法動態地將行添加到IE中的<TABLE>?
<!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,但兩者IE 7和IE 6並不表明理應被添加額外的行。我無法理解爲什麼。有誰知道這個問題的一個很好的解決方法,或者我可能做錯了什麼?
JQuery的是手工代碼另外一個非常不錯的選擇。 – GalacticCowboy 2009-05-01 19:13:02
+1使用MochiKit(我的fave庫),但你錯過了實際的答案 - 他需要追加行到IE的TBody來識別行。 – 2009-05-01 20:09:26
我沒有意識到appendChildNodes不工作 - 我一直在我自己的代碼中使用insertSiblingNodes,這是可行的 - 我已經更新了答案以反映工作解決方案。 – EoghanM 2009-05-01 20:53:52