简体   繁体   中英

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.

I am trying to find an alternative to it.

Here is my Original question: jQuery Filter row by condition

And here is the working answer (not in IE8 though):

// 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. Instead, you can use jQuery's 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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