我有一个包含字符串列表的数百行的html表。我也有一个输入框,可以在表格中插入新的行。每一行中的第二个TD元素始终是一个div内的字符串,所以在表中的行可能是这样的:使用jquery通过很多元素搜索时减慢
<tr>
<td><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>
</td>
<td class="a_string" style="width:200px;">
<div style="word-wrap:break-word;">hello world</div>
</td>
...
</tr>
我想插入新行基于从一个字符串列表,在此表用户,如果一个字符串已经在表中,它将被忽略并且不被插入。当用户字符串列表非常长且表中的行数很长时,我用来检查重复项的方法效率太低,因此对于此任务来说效率太低:
function check_rows(item) { //called on each item in input list
var tf = false;
$('#mytable tbody').children().each(function (i) {
//loop through each tr
if ($('#mytable tbody').children().eq(i).children().eq(1).hasClass('a_string')) {
//if the row is a string to compare (second td element has the class 'a_string')
if (item == $('#mytable tbody').children().eq(i).children().eq(1).children().eq(0).html()) {
//find the string within the div within second td in row "i" (current indexed row)
//compare the strings and if it is found in the table break out of the loop
tf = true;
return false;
}
}
});
//if the item is not found in any of the rows false will be returned
return tf;
}
该函数做它应该做的事(检查重复的字符串),它只是太慢了。我试图插入没有这个功能的列表,它几乎是瞬间的,所以问题在这里。
有关如何改善此功能的任何建议将不胜感激!
$('#mytable tbody')'被调用的次数太多。你有没有尝试存储参考?你能跟踪数组中的项目列表,而不是从DOM中检查项目吗? – nhahtdh 2013-02-16 01:36:41
@nhahtdh是的,我可以,我不认为这会产生任何效果。我一直不关心jquery的执行速度,因为直到现在它还没有实现。将存储对tbody的引用并将表中的项目数组保存在一起会提高我的性能? – 2013-02-16 01:43:51
有太多的DOM遍历,这有点更好 - > [** FIDDLE **](http://jsfiddle.net/tsu5c/),但不是很多? – adeneo 2013-02-16 01:46:11