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

时间:2015-06-16 12:09:14

标签: javascript html

以下代码可以在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 = "";
      }
    }
  }
}
&#13;
<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>
&#13;
&#13;
&#13;

8 个答案:

答案 0 :(得分:281)

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

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

╔═══════════════╦════════╦═════════╦═══════╦═══════════════════╦═══════╦════════╗
║    Feature    ║ Chrome ║ Firefox ║ Edge  ║ Internet Explorer ║ Opera ║ Safari ║
╠═══════════════╬════════╬═════════╬═══════╬═══════════════════╬═══════╬════════╣
║ Basic Support ║    41+ ║     17+ ║ (Yes) ║ No Support        ║    28 ║      9 ║
╚═══════════════╩════════╩═════════╩═══════╩═══════════════════╩═══════╩════════╝

您需要自己实施.startsWith。这是polyfill

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

答案 1 :(得分:13)

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

import 'core-js/es6/string';

答案 2 :(得分:3)

将以下代码添加到JS文件中对我有用:

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

答案 3 :(得分:2)

正如其他人所说的,启动和结束是ES6的一部分,在IE11中不可用。我们公司总是使用lodash库作为IE11的polyfill解决方案。 https://lodash.com/docs/4.17.4

_.startsWith([string=''], [target], [position=0])

答案 4 :(得分:0)

虽然Oka的职位运作良好,但可能有点过时了。我发现lodash可以用一个函数来解决它。如果您安装了lodash,它可能会为您节省几行。

试试吧:

import { startsWith } from lodash;

。 。

    if (startsWith(yourVariable, 'REP')) {
         return yourVariable;        
    return yourVariable;
       }      
     }

答案 5 :(得分:0)

我最近也遇到了问题。我解决了使用^的问题,类似于 jquery。说,

var str = array[a].id;
if (str.startsWith('REP')) {..........}

我们可以使用

if($("[id^=str]").length){..........}

在这里,str是元素的ID。

答案 6 :(得分:0)

将startsWith函数替换为:

yourString.indexOf(searchString, position) // where position can be set to 0

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

可以将位置设置为0,以便从表示第0个位置的起点开始匹配。

答案 7 :(得分:0)

如果使用angular2 +项目时出现问题,请遵循此方法

出现此错误时,我一直在寻找解决方案,它使我到了这里。但是这个问题似乎是特定的,但错误不是,这是一个普遍的错误。对于使用Internet Explorer的开发人员来说,这是一个常见错误。

在使用angular 2+时,我遇到了同样的问题,只需几个简单的步骤即可解决。

在Angular最新版本中, polyfills.ts 中带有注释的代码,显示了在Internet Explorer版本IE09,IE10和IE11中顺利运行所需的所有polyfills。

/** IE9, IE10 and IE11 requires all of the following polyfills. **/
//import 'core-js/es6/symbol';
//import 'core-js/es6/object';
//import 'core-js/es6/function';
//import 'core-js/es6/parse-int';
//import 'core-js/es6/parse-float';
//import 'core-js/es6/number';
//import 'core-js/es6/math';
//import 'core-js/es6/string';
//import 'core-js/es6/date';
//import 'core-js/es6/array';
//import 'core-js/es6/regexp';
//import 'core-js/es6/map';
//import 'core-js/es6/weak-map';
//import 'core-js/es6/set';

取消注释代码,它将在IE浏览器中完美运行

/** IE9, IE10 and IE11 requires all of the following polyfills. **/
import 'core-js/es6/symbol';
import 'core-js/es6/object';
import 'core-js/es6/function';
import 'core-js/es6/parse-int';
import 'core-js/es6/parse-float';
import 'core-js/es6/number';
import 'core-js/es6/math';
import 'core-js/es6/string';
import 'core-js/es6/date';
import 'core-js/es6/array';
import 'core-js/es6/regexp';
import 'core-js/es6/map';
import 'core-js/es6/weak-map';
import 'core-js/es6/set';

但是与其他浏览器相比,您可能会发现IE浏览器的性能下降:(