jQuery的$。每实在是太慢了慢了、jQuery

由网友(莫欺少年一时穷)分享简介:我试图建立与 $一个动态的组合框每个和 $('<方案>'),但它是IE浏览器很慢(需要3/4分钟,以使服务器响应后的数据),在Firefox和其他浏览器它的罚款。I am trying to build a dynamic combo box with $.each and $(''),...

我试图建立与 $一个动态的组合框每个 $('<方案>'),但它是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);

这样的东西

阅读全文

相关推荐

最新文章