for (i = 0; i < $('body > p font i').length; i++) {
    current = [$('body > p font i').eq(index), $('body > p font i').eq(index).index('body > p font u, body > p font i')];
    getState(current[1]);
}

function getState(index) {
    // Lookup the object's index, then crawl up until you find a match
    while ($('body > p font u, body > p font i').eq(--index).filter('u').length == 0);
    console.log($('body > p font u, body > p font i').eq(index).text());
}

相当简单的问题。我要针对选择器过滤器迭代jQuery结果集,直到找到匹配项,然后逐步浏览结果集。

该循环运行的时间越长,它变得越慢,几乎成倍增长。

最佳答案

您正在每次迭代中在DOM树中搜索,这是一项昂贵的操作,解决方案是缓存:

var nodes = $('body > p font i');
for (var i = 0, size = nodes.length; i < size; i++) {
    current = [nodes.eq(index),nodes.eq(index).index('body > p font u, body > p font i')];
}

09-25 16:46
查看更多