0

<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要素の子を取得できるようにするために何が欠けていますか?

4

2 に答える 2

0

このエラーは、feeTable に null 値がある場合に発生する可能性があります

var feeTable = document.getElementById("feeTable");

コンソールに FeeTable の値を書き込んでみて、null 以外の適切な値であることを確認してください。

于 2015-06-23T18:37:14.080 に答える