我有一个html元素树示例(下),我想返回数据库中的每个匹配项相关的数据。假设有5个匹配项。我需要创建5个给定元素并用javascript数据填充它们吗?
我要运行一个循环,但这看起来会降低性能(为每个匹配创建所有元素树)。相反,我可以使用给定的element(pic)用javascript填充并将其放到dom上(x次)吗?如果可能的话如何?
<!-- sample elem -->
<div class="col-12 col-md-4" style="display: none">
<div class="card my-3 mx-1">
<a href="#"><img src="" alt="img"></a>
<div class="card-body">
<div class="row">
<div class="col-12 p-1">Country</div>
<div class="col-3 p-1">State</div>
<div class="col-4 p-1">City</div>
</div>
</div>
</div>
</div>
对于15-20个元素以及仅对标记来说,性能影响并不大。
但是,如果您能证明它很慢,请知道字符串会更快。因此,一种更快的方法是:
这里是什么样子:
const products = [{ title: 'gearbox' }, { title: 'drive shaft' }, { title: 'spark plug'}]
const myTemplate = '<div class="product">{title}</div>'
const finalMarkup = products.map(({ title }) => myTemplate.replace('{title}', title))
document.getElementId('targetNode').innerHtml = finalMarkup
为了进一步阐述我的评论:常常是将元素重复插入DOM树会导致性能问题,因为每次插入新节点时,文档都需要重排。您不必担心调用/调用document.createElement()
的次数太多:这是您最不用担心的。
因此,我建议您使用一个函数来创建整个样本元素。然后,您可以在循环的每次迭代中随意调用此函数来创建整个card元素,然后将其附加到文档片段中。
伪代码:
function createCard() {
// Create the entire `sample element` as you would call it
const el = <something>;
return el;
}
// Create new document fragment to hold all the nodes
// At this point, we are NOT injecting them into the DOM yet
const fragment = new DocumentFragment();
// Go through your data and create new card for each data point
for (let i = 0; i < 5; i++) {
fragment.appendChild(createCard());
}
// Now this is when you insert the entire bulk of the content into the DOM
document.querySelector('#myInsertionTarget').appendChild(fragment);
概念验证代码如下:
// Since we are creating so many `<div>` elements
// It helps to further abstract its logic into another function
function createDivElement(classes, text) {
const div = document.createElement('div');
if (classes.length)
div.classList.add(...classes);
if (text)
div.innerText = text;
return div;
}
// Call this whenever you want to create a new card
function createCard(i) {
const colCountry = createDivElement(['col-12', 'p-1'], 'Country');
const colState = createDivElement(['col-3', 'p-1'], 'State');
const colCity = createDivElement(['col-4', 'p-1'], 'City');
const row = createDivElement(['row']);
row.appendChild(colCountry);
row.appendChild(colState);
row.appendChild(colCity);
const cardBody = createDivElement(['card-body']);
cardBody.appendChild(row);
const image = document.createElement('img');
image.alt = 'img';
// Proof-of-concept image source, you can ignore this!
image.src = `https://placehold.it/100x50?text=Image%20${i+1}`;
const imageLink = document.createElement('a');
imageLink.href = '#';
imageLink.appendChild(image);
const card = createDivElement(['card', 'my-3', 'mx-1']);
card.appendChild(imageLink);
card.appendChild(cardBody);
const outer = createDivElement(['col-12', 'col-md-4']);
// outer.style.display = 'none';
outer.appendChild(card);
return outer;
}
// Create new document fragment
const fragment = new DocumentFragment();
// In each iteration of the loop, insert the new card element into fragment
for (let i = 0; i < 5; i++) {
const el = createCard(i);
fragment.appendChild(el);
}
// When you're done generating the entire set of elements
// You can then insert the fragment into your DOM (finally!)
document.querySelector('#app').appendChild(fragment);
<div id="app"></div>