2015-06-16 25 views
99

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

+8

您可能需要使用'str.indexOf(“REP”)== 0'来代替IE。 –

+0

ES6还不是一个标准的https://kangax.github.io/compat-table/es6/有一个ES6 shim库来帮助转换https://github.com/paulmillr/es6-shim/就像ES5(包括不是一切都是shimmable) – Xotic750

回答

211

String.prototype.startsWith是最新版本的JavaScript,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; 
    }; 
} 
10

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

例子:

var text = "Format"; 
if(text.indexOf("Format") == 0) { 
    alert(text + " = Format"); 
} else { 
    alert(text + " != Format"); 
} 
+0

为什么它是最好的方法? – TylerH

+0

indexOf在不替换startsWith的方法中,如果indexOf与给定字符串中的任何位置匹配,它将返回值,我不建议使用indexOf。 –

+0

'indexOf'确实返回值,但是@Jona检查过返回值为零(即字符串在开头),这意味着它*代替'startsWith'! – SharpC

3

如果这是在角2+应用情况发生,可以在polyfills.ts只是取消注释串polyfills:

import 'core-js/es6/string'; 
1

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

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

添加下面的代码,JS文件为我工作:

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

虽然欧卡的职位是伟大的工作,它可能是一个有点过时。我发现lodash可以解决它与一个单一的功能。如果你安装了lodash,它可能会为你节省几行。

试试看:

import { startsWith } from lodash; 

。 。 。

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