ОБНОВЛЕНИЕ: Обращается к необходимости добавлять уникальные значения
Я не эксперт по DOM API, но это создаст нужные элементы, клонирует их в цикле, обновит их значения textNode, добавит их к фрагменту, а затем добавит фрагмент к DOM.
Это будет работать, пока переменные, которые вы предлагаете для текстовых узлов, верны.
// Form a single fragment outside the loop
var fragment = document.createDocumentFragment();
// Create the first div and append a textNode
var div1 = document.createElement('div');
div1.appendChild( document.createTextNode('content') );
div1.setAttribute('class','test');
// Create the second div, its inner p element, and its textNode
var div2 = document.createElement('div');
var p = document.createElement('p');
p.appendChild( document.createTextNode('content') );
div2.appendChild( p );
// Variables to store the clones of above
var clone1, clone2;
// Counter for while loop
var i = 1000;
while(i--) // someIndex ranges from 10 to possibly 1000
{
// Clone the elements we created
clone1 = div1.cloneNode(true);
clone2 = div2.cloneNode(true);
// Update the nodeValue of the first textNode in div1
clone1.firstChild.nodeValue = 'someVal[i]';
// Get the p element in div2 and update its nodeValue
clone2.firstChild.firstChild.nodeValue = 'someTxt.txt1 + anotherVal.val[i]';
// Append the elements we created, cloned and updated to the fragment
fragment.appendChild(clone1).
fragment.appendChild(clone2);
}
// Append the populated fragment to #screen1
document.getElementById('screen1').appendChild(fragment);
EDIT:
Если вы хотите манипулировать законченным фрагментом с помощью jQuery перед его добавлением, вам нужно сделать:
$(fragment.childNodes); // Create a jQuery object of the content of the fragment
, поскольку это не будет работать правильно:
$(fragment); // Doesn't work. jQuery methods will be ineffective.