下面的代碼可以在沒有在Chrome瀏覽器的問題上運行正常,但投在Internet Explorer中出現以下錯誤11代碼,在Chrome
對象不支持屬性或方法'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。 –
ES6還不是一個標準的https://kangax.github.io/compat-table/es6/有一個ES6 shim庫來幫助轉換https://github.com/paulmillr/es6-shim/就像ES5(包括不是一切都是shimmable) – Xotic750