简体   繁体   English

创建元素和设置innerHTML更有效还是直接添加HTML?

[英]Is creating element and set innerHTML more efficient or add HTML directly?

Please forgive confusing question above...请原谅上面令人困惑的问题...

I have a dashboard that will fetch json data with ajax for every couple seconds.我有一个仪表板,每隔几秒钟将使用 ajax 获取 json 数据。 The current solution is it will clear the dashboard, and get the json data and combine them with html, so is kind of like this:当前的解决方案是清除仪表板,获取 json 数据并将它们与 html 结合,所以有点像这样:

$.ajax({
    ....
}).done((res) => {
    $mainBody.html('');
    for (let i = 0; i < res.length; i++){
        let ele = '<div class="...">  with adding the json data  </div>';
        $mainBody.append(ele);
    }
})

Each ele will contain around 55 lines of html code, and most of time will loop for more than 20 times to finish adding all elements.每个ele将包含大约 55 行 html 代码,并且大部分时间会循环超过 20 次以完成添加所有元素。

And now I want to take a more object-oritented approach to this dashboard and considering to use document.createElement() for all elements, and then to set their innerHtml directly after every fetch.现在我想对这个仪表板采取更面向对象的方法,并考虑对所有元素使用document.createElement() ,然后在每次获取后直接设置它们的innerHtml But I'm not so sure on efficiency wise and speed, which solution should I use.但我不太确定效率和速度,我应该使用哪种解决方案。

The most effective would be to use document.createElement because in addition to creating the element you have control of all its properties, that is, you don't need to use querySelector, just store the created element in a variable and then add it to the DOM, innerHTML reconstructs the entire DOM while appendChild only the part of the DOM where the parent element is, besides that you have no control over the element or its children.最有效的方法是使用 document.createElement,因为除了创建元素之外,您还可以控制其所有属性,也就是说,您不需要使用 querySelector,只需将创建的元素存储在变量中,然后将其添加到在 DOM 中,innerHTML 会重建整个 DOM,而 appendChild 只是父元素所在的 DOM 部分,此外您无法控制元素或其子元素。

 const myElement = document.createElement("span"); myElement.textContent = "Hello world;". myElement;className = "message". document.body;appendChild(myElement);
 body { text-align: center; }.message { font-weight: bold; font-size: 32px; color: #222; }

"innerHTML +=..." vs "appendChild(txtNode)" "innerHTML +=..." 与 "appendChild(txtNode)"

https://betterprogramming.pub/whats-best-innertext-vs-innerhtml-vs-textcontent-903ebc43a3fc https://betterprogramming.pub/whats-best-innertext-vs-innerhtml-vs-textcontent-903ebc43a3fc

Best would be to make one big string in the loop, and assign the innerHTML once at the end.最好是在循环中创建一个大字符串,并在最后分配一次innerHTML

let html = res.map(el => `<div class="..."> add json data in here </div>`).join('');
$mainBody.innerHTML = html;

The browser is very efficient at parsing HTML, but you should just do it once.浏览器在解析 HTML 方面非常高效,但您应该只执行一次。 Your method requires converting the DOM back to HTML and re-parsing it each time through the loop.您的方法需要将 DOM 转换回 HTML 并每次通过循环重新解析它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM