英文:
How can I add items to a list without innerHTML?
问题
我在HTML中有一个空的ul元素,每次我添加或删除颜色时,都会调用ShowColors函数,该函数使用JavaScript的.innerHTML添加li项。如何在不使用.innerHTML的情况下优化这段代码?
const colorList = document.querySelector(".all-colors");
function ShowColors() {
colorList.innerHTML = savedColorsArray.map(color => `
<li class="color">
<span class="rect" data-color="${color}" style="background: ${color};"></span>
</li>`).join("");
};
我尝试使用.insertAdjacentHTML,但我将不得不更改代码的工作方式以保存每个元素,以便稍后可以删除它,并且可能必须将其保存到localStorage以使其持久化。
savedColorsArray.forEach(color => {
colorList.insertAdjacentHTML('beforeend', `<li class="color">
<span class="rect" data-color="${color}" style="background: ${color};"></span>
</li>`);
});
英文:
I have an empty ul in html and everytime I add or remove a color I call ShowColors which adds li items via JavaScript using .innerHTML. How can I optimize this code without using .innerHTML?
<!-- language: lang-js -->
const colorList = document.querySelector(".all-colors");
function ShowColors() {
colorList.innerHTML = savedColorsArray.map(color => `
<li class="color">
<span class="rect" data-color="${color}" style="background: ${color};"></span>
</li>`).join("");
};
<!-- language: lang-html -->
<ul class="all-colors"></ul>
I tried .insertAdjacentHTML but I would have to change how the code works to save each element so I can remove it later and also probably have to save it to localStorage so it's persistent.
<!-- language: lang-js -->
savedColorsArray.forEach(color => {
colorList.insertAdjacentHTML('beforeend', <li class="color">
);
<span class="rect" data-color="${color}" style="background: ${color};"></span>
</li>
答案1
得分: 1
以下是翻译好的部分:
// JavaScript 代码部分
const colorList = document.querySelector(".all-colors");
const deleteItem = item =>
savedColorsArray.splice([...colorList.children].indexOf(item.closest('.color')), 1);
let updatePromise;
let updateCount = 0;
const savedColorsArray = new Proxy([], {
set(target, prop, val){
// 在微任务中延迟执行
updatePromise ??= Promise.resolve().then(() => {
updatePromise = null;
const log = document.createElement('div');
log.textContent = `已更新 ${++updateCount} 次`;
document.querySelector('.log').appendChild(log);
let idx = 0;
const $children = [...colorList.children];
for(const color of target){
let $item = $children[idx++];
if(!$item){
const div = document.createElement('div');
div.innerHTML = `<li class="color">
<span class="rect" data-color="${color}" style="background: ${color};"><b onclick="deleteItem(this)">✕</b></span>
</li>`;
colorList.appendChild(div.children[0]);
}else{
const $span = $item.querySelector('span');
$span.dataset.color = color;
$span.style.background = color;
}
}
// 清除未使用的项目
while(idx < $children.length){
$children[idx++].remove();
}
});
return Reflect.set(target, prop, val);
}
});
savedColorsArray.push(...'red orange yellow lime green blue'.split(' '));
/* CSS 代码部分 */
.color span{
display:block;
height: 30px;
width: 150px;
border-radius:4px;
transition: background .5s;
position:relative;
}
.all-colors{
padding:0;
margin:0;
}
b{
position:absolute;
right:5px;
top:3px;
cursor:pointer;
}
.color{
padding: 5px;
display:block;
list-style:none;
}
body{
display:flex;
gap:30px;
}
<!-- HTML 代码部分 -->
<ul class="all-colors"></ul>
<div>
<input>
<button onclick="document.querySelector('input').value.match(/\w+/g)?.forEach(color => savedColorsArray.unshift(color))">添加颜色</button>
</div>
<div class="log"></div>
英文:
A modern approach could be a reactive solution with Proxy
:
you just modify an array of colors and the list is updated automatically.
You just iterate the color array and try to reuse existing <li>
. If there's a <li>
with the same index as a color, update it with dataset.color = color
and style.background = color
. Otherwise create an <li>
with innerHTML
like you did. The reusing of DOM elements seems like optimization like you requested.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const colorList = document.querySelector(".all-colors");
const deleteItem = item =>
savedColorsArray.splice([...colorList.children].indexOf(item.closest('.color')), 1);
let updatePromise;
let updateCount = 0;
const savedColorsArray = new Proxy([], {
set(target, prop, val){
// postpone in a microtask
updatePromise ??= Promise.resolve().then(() => {
updatePromise = null;
const log = document.createElement('div');
log.textContent = `updated ${++updateCount}`;
document.querySelector('.log').appendChild(log);
let idx = 0;
const $children = [...colorList.children];
for(const color of target){
let $item = $children[idx++];
if(!$item){
const div = document.createElement('div');
div.innerHTML = `<li class="color">
<span class="rect" data-color="${color}" style="background: ${color};"><b onclick="deleteItem(this)">✕</b></span>
</li>`;
colorList.appendChild(div.children[0]);
}else{
const $span = $item.querySelector('span');
$span.dataset.color = color;
$span.style.background = color;
}
}
// clear unused items
while(idx < $children.length){
$children[idx++].remove();
}
});
return Reflect.set(target, prop, val);
}
});
savedColorsArray.push(...'red orange yellow lime green blue'.split(' '));
<!-- language: lang-css -->
.color span{
display:block;
height: 30px;
width: 150px;
border-radius:4px;
transition: background .5s;
position:relative;
}
.all-colors{
padding:0;
margin:0;
}
b{
position:absolute;
right:5px;
top:3px;
cursor:pointer;
}
.color{
padding: 5px;
display:block;
list-style:none;
}
body{
display:flex;
gap:30px;
}
<!-- language: lang-html -->
<ul class="all-colors"></ul>
<div>
<input>
<button onclick="document.querySelector('input').value.match(/\w+/g)?.forEach(color => savedColorsArray.unshift(color))">Add color</button>
</div>
<div class="log"></div>
<!-- end snippet -->
答案2
得分: 0
.innerHTML
应尽量避免使用,因为调用它会触发 HTML 解析器,重新构建带有新内容的 DOM,因此使用它会带来性能问题。
在重新构建带有新内容的 DOM 时,很容易清除先前添加的事件处理程序,因此存在引入错误的可能性。
众所周知,当传递给 .innerHTML
的字符串不完全受开发人员控制时,它可能会在代码中引发安全漏洞。
来自 MDN:
警告:如果您的项目将接受任何形式的安全审查,那么在大多数情况下使用 innerHTML 可能会导致您的代码被拒绝。例如,如果您在浏览器扩展中使用 innerHTML 并将扩展提交到 addons.mozilla.org,则在审查过程中可能会被拒绝。请参阅将外部内容安全地插入页面以获取替代方法。
所以,故事的寓意是,只有在没有其他选择时才使用 .innerHTML
。幸运的是,通常还有其他选择,其中首选方法之一是创建新的 DOM 元素,配置它们,然后将它们附加到 DOM。
以下是如何实现的示例:
const colorList = document.querySelector(".all-colors");
function ShowColors() {
// 创建新的 DOM 元素
const li = document.createElement("li");
const span = document.createElement("span");
// 通过 DOM 属性配置元素
li.classList.add("color");
span.classList.add("rect");
span.style.backgroundColor = color;
span.dataset.dataColor = color;
// 将新的 DOM 元素附加到适当的父元素上
li.appendChild(span);
colorList.appendChild(li);
};
英文:
.innerHTML
should be avoided whenever possible because calling it invokes the HTML parser, which rebuilds the DOM with the new content, so there are performance implications to using it.
While the DOM is being rebuilt with new content, it's very easy to wipe out previously added event handlers, so there is an opportunity to introduce bugs.
It is well known that using .innerHTML
can potentially open up a security vulnerability in code as well, when the string that is passed to it is not 100% within the control of the developer.
From MDN:
> Warning: If your project is one that will undergo any form of security
> review, using innerHTML most likely will result in your code being
> rejected. For example, if you use innerHTML in a browser extension and
> submit the extension to addons.mozilla.org, it may be rejected in the
> review process. Please see Safely inserting external content into a
> page for alternative methods.
So, the moral of the story is, only use .innerHTML
when you have no other choice. Fortunately, there often are other choices, the first among them being to create new DOM elements, configure them, and then attach them to the DOM.
Here's how that is done:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const colorList = document.querySelector(".all-colors");
function ShowColors() {
// Create a new DOM elements
const li = document.createElement("li");
const span = document.createElement("span");
// Configure the elements via DOM properties
li.classList.add("color");
span.classList.add("rect");
span.style.backgroundColor = color;
span.dataset.dataColor = color:
// Append the new DOM element to the appropriate parent elements
li.appendChild(span);
colorList.appendChild(li);
};
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论