รหัสต่อไปนี้สามารถทำงานได้โดยไม่มีปัญหาใน Chrome แต่เกิดข้อผิดพลาดต่อไปนี้ใน Internet Explorer 11
วัตถุไม่สนับสนุนคุณสมบัติหรือวิธีการ 'startsWith'
ฉันกำลังจัดเก็บ ID ขององค์ประกอบในตัวแปร ปัญหาคืออะไร
function changeClass(elId) {
var array = document.getElementsByTagName('td');
for (var a = 0; a < array.length; a++) {
var str = array[a].id;
if (str.startsWith('REP')) {
if (str == elId) {
array[a].style.backgroundColor = "Blue";
array[a].style.color = "white";
} else {
array[a].style.backgroundColor = "";
array[a].style.color = "";
}
} else if (str.startsWith('D')) {
if (str == elId) {
array[a].style.backgroundColor = "Blue";
array[a].style.color = "white";
} else {
array[a].style.backgroundColor = "";
array[a].style.color = "";
}
}
}
}
<table>
<tr>
<td id="REP1" onclick="changeClass('REP1');">REPS</td>
<td id="td1"> </td>
</tr>
<tr>
<td id="td1"> </td>
<td id="D1" onclick="changeClass('D1');">Doors</td>
</tr>
<tr>
<td id="td1"> </td>
<td id="D12" onclick="changeClass('D12');">Doors</td>
</tr>
</table>
str.indexOf("REP") == 0
IE แทน