我有一张 table ,我对它进行了排序。它的执行时间很糟糕。我猜这是因为 DOM 操作。 /* 我正在转换为数组 */
var rows = $table.find('tbody > tr').get();
$.each(rows, function(index, row){ /*then again to 2D array */
if(($(row).children('td').eq(0).attr('class').indexOf('collapse') != -1 || $(row).children('td').eq(0).attr('class').indexOf('expand') != -1)){
myData.push(myData1);
myData1 = [];
}
myData1.push(row);
rowCount++;
if(rowCount == $(rows).length){ // to assign last group of rows
myData.push(myData1);
myData1 = [];
}
});
这是直接通过数组选择 DOM 元素的最佳方式。因为我多次使用这个。
最佳答案
嗯,您可以立即做一件事,这可能会提高性能:
你看到长链,你必须获取第一个 <td>
的类值行的?你正在做同样的事情,长长的,连续两次。尝试一下
// then again to 2D array
$.each(rows, function(index, row) {
var classes = $(row).children('td').eq(0).attr('class')
if((classes.indexOf('collapse') != -1 || classes.indexOf('expand') != -1)){
myData.push(myData1);
myData1 = [];
}
myData1.push(row);
rowCount++;
if(rowCount == $(rows).length){ // to assign last group of rows
myData.push(myData1);
myData1 = [];
}
});
如果$.each()
函数运行了很多次,这至少可以为您节省一些时间。
关于jquery - jquery 中的 DOM 选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/975245/