Các mã sau đây có thể chạy mà không có một vấn đề trong Chrome, nhưng ném các lỗi sau trong Internet Explorer 11.Mã không chạy trong trình duyệt IE 11, hoạt động tốt trong Chrome
Object không hỗ trợ tài sản hoặc phương pháp 'startsWith'
Tôi đang lưu trữ ID của phần tử trong một biến. Vấn đề là gì?
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>
Có thể bạn sẽ cần sử dụng 'str.indexOf (" REP ") == 0' cho IE để thay thế. –
ES6 chưa một tiêu chuẩn https://kangax.github.io/compat-table/es6/ Có một thư viện ES6 shim để giúp quá trình chuyển đổi https://github.com/paulmillr/es6-shim/ Cũng giống như cho ES5 (bao gồm không phải tất cả mọi thứ là shimmable) – Xotic750