我必须迭代一组<ul>
有<li>
s 并根据输入的单词对其进行过滤。基于 id 的第一个代码运行良好。但问题是我们有重复的 ID,这破坏了过滤器。因此,我为班级分配了 ID 并进行了班级选择,现在它运行良好,但存在一些问题。主要问题是类选择过滤器比 Id 选择过滤器慢。因为它是一个很大的列表,所以在使用类名进行迭代时我会遇到缓慢。有什么办法可以让它更快吗?我也想知道为什么选课慢!
这是我基于 id 迭代的代码:
$.each(propertiesList, function () {
var item = $("#" + this.id).parent();
if(this.property.toLowerCase().indexOf(value.toLowerCase()) === -1) {
item.addClass('no-match');
}
else {
item.parents('.root-property-category').show().addClass('expanded').children('ul').show();
item.removeClass('no-match');
}
});
这是基于类的迭代:
$.each(propertiesList, function () {
var item = $("#available-properties [class*='" + this.id + "']").parent();
if(this.property.toLowerCase().indexOf(value.toLowerCase()) === -1) {
console.log(item)
item.addClass('no-match');
}
else {
console.log(item)
item.parents('.root-property-category').show().addClass('expanded').children('ul').show();
item.removeClass('no-match');
}
});