[英]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.