目前我有一个循环在每次迭代中更新 DOM;我了解到这是一种不好的做法,您应该尽可能少地更新 DOM 以获得更好的速度。
所以我想知道如何编辑下面的内容,以便我可以将所有元素存储在一个元素或其他东西中,然后在循环结束后添加一个 DOM。
这是循环..
for (var i = spot; i < spot + batchSize && i < cats.options.length; i++) {
// Check if the cat is selected
if (cats.options[i].selected == true) {
// Set this category's values to some variables
var cat_id = cats.options[i].getAttribute('value');
var cat_name = cats.options[i].text;
if (checkCatSICAdd(cat_id) === false) {
// Now we create the new element
var new_option = document.createElement('option');
// Add attribute
new_option.setAttribute('value',cat_id);
// Create text node
var new_text_node = document.createTextNode(cat_name);
// Append new text node to new option element we created
new_option.appendChild(new_text_node);
// Append new option tag to select list
sel_cats.appendChild(new_option);
} else {
failed++;
}
}
}