代码未在IE 11中运行,在Chrome中运行良好

Bhu*_*ere 149 html javascript

以下代码可以在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 = "";
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)
<table>
  <tr>
    <td id="REP1" onclick="changeClass('REP1');">REPS</td>
    <td id="td1">&nbsp;</td>
  </tr>
  <tr>
    <td id="td1">&nbsp;</td>
    <td id="D1" onclick="changeClass('D1');">Doors</td>
  </tr>
  <tr>
    <td id="td1">&nbsp;</td>
    <td id="D12" onclick="changeClass('D12');">Doors</td>
  </tr>
</table>
Run Code Online (Sandbox Code Playgroud)

Oka*_*Oka 303

String.prototype.startsWith 是最新版本的JavaScript ES6中的标准方法.

查看下面的兼容性表,我们可以看到除了 Internet Explorer版本之外的所有当前主要平台都支持它.

?????????????????????????????????????????????????????????????????????????????????
?    Feature    ? Chrome ? Firefox ? Edge  ? Internet Explorer ? Opera ? Safari ?
?????????????????????????????????????????????????????????????????????????????????
? Basic Support ?    41+ ?     17+ ? (Yes) ? No Support        ?    28 ?      9 ?
?????????????????????????????????????????????????????????????????????????????????
Run Code Online (Sandbox Code Playgroud)

你需要.startsWith自己实现.这是polyfill:

if (!String.prototype.startsWith) {
  String.prototype.startsWith = function(searchString, position) {
    position = position || 0;
    return this.indexOf(searchString, position) === position;
  };
}
Run Code Online (Sandbox Code Playgroud)

  • [MDN web docs](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith#Polyfill)中还有另一种实现。`String.prototype.startsWith= function(搜索,pos){返回this.substr(!pos || pos &lt;0?0:+ pos,search.length)===搜索; };` (3认同)

小智 33

text.indexOf("newString")是最好的方法,而不是startsWith.

例:

var text = "Format";
if(text.indexOf("Format") == 0) {
    alert(text + " = Format");
} else {
    alert(text + " != Format");
}
Run Code Online (Sandbox Code Playgroud)

  • `indexOf`确实返回值,但是@Jona检查了返回值为零(即字符串在开头),这意味着**是`startsWith`的一个很好的替代品! (9认同)

Vit*_*kov 13

如果在Angular 2+应用程序中发生这种情况,您可以在polyfills.ts中取消注释字符串polyfill:

import 'core-js/es6/string';
Run Code Online (Sandbox Code Playgroud)


Har*_*ant 7

将 startsWith 函数替换为:

yourString.indexOf(searchString, position) // where position can be set to 0
Run Code Online (Sandbox Code Playgroud)

这将支持包括 IE 在内的所有浏览器

位置可以设置为 0 从开始的字符串匹配,意思是第 0 个位置。