下面的代码可以在没有在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