簡體   English   中英

原型功能在IE8中不起作用

[英]Prototype function not working in IE8

我最近問了一個問題,並收到了有效的工作答案。 但是當時,我正在Firefox中進行測試,盡管看上去一切都不錯,但所需的瀏覽器IE8不喜歡此功能。

我正在嘗試尋找替代方案。

這是我的原始問題: jQuery按條件過濾行

這是有效的答案(盡管不是在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();

我對原型一無所知,所以我按照它的需要去做,但是歡迎其他解決方案。

如評論中所述。 Array.prototype.filter在IE8中不存在。 相反,您可以使用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();

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM