jQuery的$。每实在是太慢了 [英] jQuery $.each is really slow

查看:138
本文介绍了jQuery的$。每实在是太慢了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立与 $一个动态的组合框每个 $('<方案>'),但它是IE浏览器很慢(需要3/4分钟,以使服务器响应后的数据),在Firefox和其他浏览器它的罚款。

I am trying to build a dynamic combo box with $.each and $('<OPTION>'), but it is really slow on IE (takes 3/4 mins to render data after server response) on firefox and other browsers it's fine.

下面是我的code,它构建组合

Here is my code that builds combo

var sel = ('#myDynCmb');
$.each(dataCollection, function(key,_value) {
    sel.append($("<OPTION>").val(key).text(_value));
});

任何帮助AP preciated。

Any help appreciated.

推荐答案

DOM操作是usualy缓慢,特别是具有当你追加到DOM。

Dom manipulation are usualy slow, especialy when you're appending to the dom.

一个很好的做法是把所有的HTML到一个变种并追加该变种的内容到DOM,至极的结果在一个DOM操作,这是更快

One good practices is to put all your html into a var and append the content of this var to the dom, wich result in one dom opération, this is much faster

var htmlToAppend = "<select>";
$.each(dataCollection, function(key,_value) {
    select += "<option value="+key+">"+_value+"</option>";
});
htmlToAppend += "</select>";
$('#myDynCmb').empty().append(htmlToAppend);

这样的东西

这篇关于jQuery的$。每实在是太慢了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆