The difference between 'innerHTML' and 'appendChild'

旧街凉风 提交于 2019-12-01 05:15:28

Using appendChild adds a new DOM element to the end of the parent node.

Using innerHTML += takes the existing DOM content of the parent node, serialises it to HTML in a string, adds some more HTML to the end of the string, erases the existing elements in the parent node, generates DOM elements from that string, then appends the new nodes to the parent node.

Difference between innerHTML and appendChild Jquery / Javascript : What is the difference between innerHTML and appendChild?

Answer :

appendChild is used to insert new node in DOM. innerHTML is a property of DOM that allows to replace content of an element with different HTML, which automatically gets parsed into DOM nodes.

please_read_the link

innerHTML will replace all the content, and addChild will add a child to the existing ones i think

innerHTML will just update the DOM html element node you are referring to.

'appendChild' on the other hand, will add a nested DOM HTML element (child) node to your "parent" element in context. for example: for a <ul> it will append it's child - that is <li>

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!