我想獲得ID(從我不知道它是字符串還是元素)。我的HTML代碼和Javascript是這樣的:從表格輸入中獲取元素ID
var table = document.getElementById('workerTimes');
var emptyArray = new Array(100);
for (var r = 1, n = table.rows.length; r < n; r++) {
var element = table.rows[r].cells[0].innerHTML;
emptyArray.push(element.id);
console.log(element);
}
<table class="table" id="workerTimes">
<tbody>
<tr>
<th></th>
<th>Osoba</th>
<th>Rozpoczął pracę punktualnie</th>
<th>Zakończył pracę punktualnie</th>
</tr>
<tr>
<td><input type="checkbox" id="50"></td>
<td>Jan</td>
<td>
<button type="button" class="button_positive" onclick="tickClick(50, 0, 0)">✓</button>
<button type="button" class="button_negative" onclick="tickClick(50, 0, 1)">X</button>
</td>
<td>
<button type="button" class="button_positive" onclick="tickClick(50, 1, 0)">✓</button>
<button type="button" class="button_negative" onclick="tickClick(50, 1, 1)">X</button>
</td>
</tr>
</tbody>
</table>
和控制檯日誌顯示
<輸入類型= 「複選框」 ID = 「50」>
但我無法獲得此元素的ID並推送到數組。這個怎麼做?
element.innerHTML是文本。你想使用element.children或潛在的element.childNodes – JonSG