我最近问了一个问题并收到了有效的工作答案。然而当时,我在Firefox中进行测试,虽然看起来都不错,但是所需的浏览器IE8并不喜欢这个功能。
我正试图寻找替代方案。
以下是我的原始问题:jQuery Filter row by condition
这是工作答案(不是在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();
我根本不了解原型,所以我只是按照我的需要去做,但欢迎任何其他解决方案。
答案 0 :(得分:1)
如评论中所述。 IE8中不存在Array.prototype.filter
。相反,您可以使用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+/)
,如上所述。