<tr>
テーブル内の要素の子を取得しようとしています。個々の<td>
セルをそれぞれ取得する必要があります。jQuery は使用できず、インフラストラクチャの理由から JavaScript のみを使用します。私の JavaScript は次のようになります。
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
要素の子を取得できるようにするために何が欠けていますか?