Javascript 原型功能在IE8中不起作用

Javascript 原型功能在IE8中不起作用,javascript,jquery,internet-explorer-8,Javascript,Jquery,Internet Explorer 8,我最近问了一个问题,得到了一个有效的工作答案。然而,当时我正在Firefox中测试,虽然一切看起来都不错,但所需的浏览器IE8并不喜欢这个功能 我正试图找到一个替代方案 这是我原来的问题: 以下是有效的答案(但不是在IE8中): 我对prototype一点也不了解,所以我只是按照我的需要使用它,但欢迎使用任何其他解决方案。如评论中所述数组.prototype.filter在IE8中不存在。相反,您可以使用jQuery的grep(): 请注意,.classList在IE中也不受支持,直到IE 10

我最近问了一个问题,得到了一个有效的工作答案。然而,当时我正在Firefox中测试,虽然一切看起来都不错,但所需的浏览器IE8并不喜欢这个功能

我正试图找到一个替代方案

这是我原来的问题:

以下是有效的答案(但不是在IE8中):


我对prototype一点也不了解,所以我只是按照我的需要使用它,但欢迎使用任何其他解决方案。

如评论中所述<代码>数组.prototype.filter在IE8中不存在。相反,您可以使用jQuery的
grep()


请注意,
.classList
在IE中也不受支持,直到IE 10。相反,您可以使用
this.className.split(/\s+/)
,如上所述。

Array.prototype。ie8中不存在筛选器。也许你可以改用jQuery grep,IE在IE9中获得了对
Array.prototype.filter
的支持…@JamesThorpe-谢谢James,我在做一些研究时确实看到了这一点,但遗憾的是,在接下来的几个月里,我们仍然在使用IE8,这一产品需要支持它。我只是没有一个OP的解决方案,所以@HMR建议,尝试修复这个问题时,会有一个jQuery替代方案,或者有一个@JamesThorpe-感谢链接,我将代码添加到我的工具中,虽然错误消失了,但函数没有。。功能。我将尝试并找到一个jQuery替代方案,我认为IE8出于某种原因说
长度为null或不是对象
?我不知道那里的任何代码如何导致该错误,除非错误发生在jQuery内部?您是否已在调试器中逐步完成了代码?错误发生在哪一行?是的,我正在通过它,它发生在tr行的开始。它引用了错误中的核心jquery文件。这是在GREP行上发生的。在Filter函数中,我把它作为断点,并测试了
这个
,事实上我们在行中,类就在那里。但是,this.classList是否应该返回任何内容?我不确定classList是否是
grep
的一部分,但当我在filter函数中执行它时,会说undefined。IE中似乎不支持类列表:@SBB Yes,
。问题出在类列表上。请看我的最新答案。
// collating the 'regions':
var regions = ['americas', 'emea', 'apac'],
// initialising an array to use, later:
    foundClasses = [];

// iterating over the 'tr' elements, filtering them:
$('tr').filter(function () {
    // using Array.prototype.forEach to filter the classList of the element:
    foundClasses = Array.prototype.filter.call(this.classList, function (c) {
        // 'c' is the current class in the classList we're iterating over,
        // if it's in the array we return that array to the 'foundClasses':
        if (regions.indexOf(c) > -1) {
            return c;
        }
    });
    // we keep the the element in the jQuery collection (of 'tr' elements),
    // if we do not have exactly 2 of the allowed classes...
    return foundClasses.length !== 2;
// removing those 'tr' elements:
}).remove();
// collating the 'regions':
var regions = ['americas', 'emea', 'apac'];

// iterating over the 'tr' elements, filtering them:
$('tr').filter(function () {
    var foundClasses = $.grep(this.className.split(/\s+/), function (c) {
        return $.grep(regions, function(r) { return r === c; }).length > 0;
    });

    // we keep the the element in the jQuery collection (of 'tr' elements),
    // if we do not have exactly 2 of the allowed classes...
    return foundClasses.length !== 2;
}).remove();