0
我有一个这样的功能在JQuery和JS。我有一个带有复选框的div列表,并将它们添加到我的列表中。这适用于40个div,但有时我有2000个,它会崩溃Chrome并在FF上爬行。无论如何,让这个更快?慢JQuery函数
function AddToList()
{
$('div[name="notadded"] input:checked').each(function(index)
{
var html = $(this).parents('div[name="notadded"]').html();
//get rid of the class that is used to gather checkboxes in select/deselect
html = html.replace('listvars', 'addedvars');
var var_id = $(this).attr('value');
var new_html = '<div id="added_' + var_id + '" name="added">' + html + '</div>';
//hide the one we are adding and remove the check
$(this).parents('div[name="notadded"]').hide();
$('div[name="notadded"] input[value="' + var_id + '"]').attr('checked', false);
//add the vars to the added list
$('.addedList').append(new_html);
step3 = '3b';
});
}
听起来像问题是与你的HTML,而不是你的JavaScript。为什么你需要2000个div?必须有许多方法来优化。 – Blazemonger 2012-07-19 19:58:46
你也有名称属性的div? – Chandu 2012-07-19 19:59:08
如果您想做出如此大的替换,请尝试一次完成:构建一个大的html(而不是2000个),并用一个附加内容替换整个集合。 – 2012-07-19 20:00:43