简体   繁体   English

插入内联HTML或创建新元素是否更快?

[英]Is it faster to insert inline HTML or create a new element?

I have a question, I use prototype, but maybe this question could apply to native javascript as well. 我有一个问题,我使用原型,但也许这个问题也适用于本机javascript。

Which would be generally faster and more efficient to run: 通常,这将更快,更高效地运行:

$(divElement).insert('<div>Hello</div');

or 要么

$(divElement).insert(new Element(div).insert('Hello'));

How about creating the element seperatly and assigning it to a variable like this: 如何分别创建元素并将其分配给这样的变量:

var helloDiv = '<div>Hello</div>';
$(divElement).insert(helloDiv);

or 要么

var helloDiv = new Element('div').insert('Hello');
$(divElement).insert(helloDiv);

Is creating inline html faster than creating an element and then inserting it? 创建内联html比创建元素然后插入它快吗? This information could be useful especially for constructing something like tables. 该信息对于构造诸如表之类的信息尤其有用。

Thank you and appreciate your help. 谢谢,感谢您的帮助。

innerHTM L is faster than appendChild innerHTM L比appendChild

The advantage of appendChild is that it actually updates the DOM properly in all browsers so that you can actually read back and update the tags you added. appendChild的优点是,它实际上在所有浏览器中都正确地更新了DOM,因此您实际上可以读回并更新添加的标签。

Using innerHTML does not update the DOM in all browsers and so the content added that way may not be able to be updated after it is added (if you need to do that). 使用innerHTML并不会在所有浏览器中更新DOM,因此添加后的内容可能无法在添加后更新(如果需要的话)。

它取决于浏览器,对于ie和firefox来说绝对正确,但在chrome上似乎较慢: http : //jsperf.com/innerhtml-vs-createelement-test

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

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