Jquery - .forEach() 在 IE8 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16186930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery - .forEach() not working in IE8
提问by Milo-J
I have created this little interaction for one of the platforms at work - http://jsfiddle.net/S79qp/426/
我为工作中的一个平台创建了这个小交互 - http://jsfiddle.net/S79qp/426/
It works fine in all browsers apart form IE8. When I run the console it seems to be this section that it is having problems with...
它在除 IE8 之外的所有浏览器中都能正常工作。当我运行控制台时,它似乎是这个部分有问题......
Array.prototype.forEach.call(l, function(item) {
a.push(jQuery(item).text());
});
Can someone show me an IE8 friendly alternative so I can make it compatible for the versions required?
有人可以向我展示一个 IE8 友好的替代品,以便我可以使其与所需的版本兼容吗?
回答by Chad Hedgcock
If all you want is forEach()
in IE8:
如果您想要的只是forEach()
在 IE8 中:
if (typeof Array.prototype.forEach != 'function') {
Array.prototype.forEach = function(callback){
for (var i = 0; i < this.length; i++){
callback.apply(this, [this[i], i, this]);
}
};
}
This will behave as expectedin any browser that doesn't have it built-in.
这将在任何没有内置它的浏览器中按预期运行。
回答by Guffa
Use the jQuery.each
method:
jQuery.each(l, function(index, item){
a.push(jQuery(item).text());
});
If the target array is empty from start, you can use the jQuery.map
methodfor this instead:
如果目标数组从一开始就是空的,你可以使用这个jQuery.map
方法来代替:
var a = jQuery.map(l, function(item){
return jQuery(item).text();
});
回答by veritascs
forEach is not supported in IE 8, instead you can use a regular loop:
IE 8 不支持 forEach,您可以使用常规循环:
for ( var i = 0; i < myArray.length; i++ ) {
// code
}
回答by Laurent S.
Indeed the forEach method is only available from IE9. You should use the jQuery version "each()" in order to offer support to older browsers.
事实上,forEach 方法仅在 IE9 中可用。您应该使用 jQuery 版本“each()”以便为旧浏览器提供支持。
回答by Magige Daniel
I had the same issue with IE8 and here is how I solved it!
我在 IE8 上遇到了同样的问题,这是我解决的方法!
First of all wanted to loop and get data from JSON arrayobject. Take look of original which is working well in Firex, chrome and latest IE but not IE8
首先想要循环并从JSON 数组对象中获取数据。看看在 Firex、chrome 和最新的 IE 中运行良好但在 IE8 中运行良好的原始版本
data.children.forEach(function(item) {
//javascript: console.log(item);
console.log(data.children);
attachRel(item, '1' + (data.children.length > 1 ? 1 : 0));
});
Here is the solution that I developed after a long day of struggle
这是我经过一整天的斗争后开发的解决方案
$.each(data.children, function () {
attachRel(this, '1' + (data.children.length > 1 ? 1 : 0));
});