だから、次のような動的に生成されるテーブルを含むページがあります
<table id="enProps">
<tr>
<td class="en_US">
<input readonly="readonly" value="shipbillpayplacerequisition" type="text">
</td>
<td class="en_US">
<input class="longField" readonly="readonly" value="Ship, Bill & Pay : Place Requisition" type="txt">
</td>
<td style="display: none;" class="otherIds">
<input class="identifier" readonly="readonly" value="shipbillpayplacerequisition" type="text">
</td>
<td class="keyValue">
<input class="value longField" id="shipbillpayplacerequisition" value="" type="text">
</td>
</tr>
</table>
行数は、入力ボックスの値フィールドに格納される内容と同様にさまざまです。2 つのフィールドの「値」の値を比較する単純なスクリプトをバニラ JavaScript で記述しようとしています (読み取り: JQuery などの外部ライブラリはありません)。
これまでのところ、私が持っているのは
var table = document.getElementById("enProps");
var rowCount = table.getElementsByTagName("TR").length;
var engValue;
var frenchValue;
for (var i = 0; i < rowCount; i++){
var row = table.rows[i];
var cellCount = row.cells.length;
for (var j = 0; j < cellCount; j++){
var cell = row.cells[j];
if (cell.getAttribute("class") == "en_US"){
var inputs = cell.getElementsByClassName("longField")[0].getAttribute("value");
}
}
}
これを実行しようとするたびに、次のエラーが表示されます
cell.getElementsByClassName("longField")[0] is undefined
奇妙なのは、getAttribute メソッドの呼び出しを削除し、alert または console.log を使用して入力の値を出力すると、正しい要素が表示されることです。.firstChild、.childNodes[0] を使用したり、メソッド呼び出しを文字列化しないなど、さまざまなバリエーションを試しました。.getAttribute が関数ではないか、何かが未定義であり、常にその行の .getAttribute 呼び出しが原因であるというエラーが発生するたびに、セレクターで console.log を呼び出すたびに適切な入力が表示されるのが奇妙です鬼ごっこ。私の質問は、入力タグの「値」クラスの値を取得するために使用しようとすると、.getAttribute がエラーになるのはなぜですか?