是否建议使用innerHTML + =? [英] Is using innerHTML += advisable?

查看:26
本文介绍了是否建议使用innerHTML + =?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Web应用程序中,首先下载了所需的初始div.然后,我发送一个请求,并使用ajax调用获取其他绝对div(弹出div/警告msg div等).现在我要做的是下面的

In my web application, The initial divs required are downloaded first. Then, I send a request and get the additional absolute divs(pop up divs/warning msg divs etc.,) using ajax call. Now what I do is the below

document.body.innerHTML += secondarydivs;

这会导致性能下降,因为主体已经被构造好了,现在我将添加更多的div?我真正需要知道的是,当执行该语句时,是重新绘制整个文档还是仅将附加字符串解析为添加到DOM?

Will this cause a performance hit as the body is already constructed and now I am appending even more divs? What I exactly need to know is, when this statement is excecuted, is the whole document repainted or only the additonal string gets parsed added to the DOM?

谢谢!

推荐答案

它将把DOM序列化为HTML,销毁所有现有节点(丢失绑定的事件处理程序),然后从HTML.

It will serialize the DOM to HTML, destroy all the existing nodes (losing bound event handlers in the process), then recreate them (and the new nodes) from the HTML.

使用 appendChild 和朋友.

这篇关于是否建议使用innerHTML + =?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆