简体   繁体   English

原型功能在IE8中不起作用

[英]Prototype function not working in IE8

I recently asked a question and received a valid working answer. 我最近问了一个问题,并收到了有效的工作答案。 However at the time, I was testing in Firefox and while all seemed good, the required browser IE8 didnt like this function. 但是当时,我正在Firefox中进行测试,尽管看上去一切都不错,但所需的浏览器IE8不喜欢此功能。

I am trying to find an alternative to it. 我正在尝试寻找替代方案。

Here is my Original question: jQuery Filter row by condition 这是我的原始问题: jQuery按条件过滤行

And here is the working answer (not in IE8 though): 这是有效的答案(尽管不是在IE8中):

// 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();

I don't know much about prototype at all so I just went with it as it did what I needed but any other solutions are welcome. 我对原型一无所知,所以我按照它的需要去做,但是欢迎其他解决方案。

As stated in the comments. 如评论中所述。 Array.prototype.filter doesn't exist in IE8. Array.prototype.filter在IE8中不存在。 Instead, you can use jQuery's grep() : 相反,您可以使用jQuery的grep()

// 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();

Note that .classList is also not supported in IE until IE 10. Instead, you can use this.className.split(/\\s+/) , as above. 请注意,直到IE 10,IE也不支持.classList 。相反,您可以如上所述使用this.className.split(/\\s+/)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM