[英]Javascript code is working in Firefox but not in IE or Safari
The following code is working with Firefox, but neither with IE 10 nor with Safari: 以下代码适用于Firefox,但不适用于IE 10和Safari:
function showids(id) { //new
divs = document.getElementsByTagName('div');
for ( var i = 0; i < divs.length; i++ ) {
if(divs[i].id.startsWith('id1')) {
alert(divs[i].id);
if(divs[i].id.startsWith('id1'+id))
divs[i].className='one';
else {
divs[i].className='two';
alert('-'+divs[i].id);
}
}
}
}
Does anybody know the reason? 有人知道原因吗?
startsWith
isn't supported in IE or Safari. IE或Safari不支持
startsWith
。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith
You can replace the line 您可以更换线
if(divs[i].id.startsWith('id1')) {
for the follow line, using an alternative for startsWith 对于下一行,为startsWith使用替代方法
if(divs[i].id.indexOf('id1')==0) {
I added this Code from developer.mozzilla.org: 我从developer.mozzilla.org添加了以下代码:
if (!String.prototype.startsWith) {
String.prototype.startsWith = function(searchString, position) {
position = position || 0;
return this.indexOf(searchString, position) === position;
};
}
Now my Code is Working. 现在我的代码正在运行。 Thanks to all
谢谢大家
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.