我試圖讓表中的<tr>
元素的子元素。我需要得到每個個人<td>
單元格。我不能使用jQuery,只有基於基礎設施原因的JavaScript。我的JavaScript看起來像這樣:IE9兼容模式錯誤:無法獲取屬性「0」的值:對象爲空或未定義
var feeTable = document.getElementById("feeTable");
function gatherRowData() {
console.log("gathering row data");
var rows = this.parentNode.children; //This is where it fails.
var state = rows[0].innerText;
var county = rows[1].innerText;
var city = rows[2].innerText;
console.log("state: " + state);
document.getElementById("state_code").value = state;
document.getElementById("county_name").value = county;
document.getElementById("city_name").value = city;
}
//Loop through Vehicle Fee Changes to find any empty cells in county, city, or start date.
for (var i = 0; row = feeTable.rows[i]; i++) {
var county = row.cells[1];
var city = row.cells[2];
var startDate = row.cells[6];
if (county.innerText == "") {
county.style.backgroundColor = "#FF0000";
showError(invalidCells);
}
if (city.innerText == "") {
city.style.backgroundColor = "#FF0000";
showError(invalidCells);
}
if (startDate.innerText == "") {
startDate.style.backgroundColor = "#FF0000";
showError(invalidCells);
}
if (document.addEventListener) {
row.cells[8].addEventListener("click", gatherRowData);
} else if (document.attachEvent) { //Support for IE
row.cells[8].attachEvent("onclick", gatherRowData);
}
}
的onclick監聽器工作得很好,但是當我嘗試獲取錶行的孩子,它給我的錯誤:Error message: Unable to get value of the property '0': object is null or undefined
它工作正常所有其他瀏覽器和IE9不兼容模式(需要以兼容模式運行)。我錯過了什麼可以讓我得到tr
元素的孩子?當feeTable具有空值
var feeTable = document.getElementById("feeTable");
控制檯feeTable的嘗試寫值可能會出現
也許試試'.parentNode.getElementsByTagName('td')'而不是'.parentNode.children' –