What is better, appending new elements via DOM functions, or appending strings with HTML tags?

后端 未结 6 1128
无人共我
无人共我 2020-11-29 05:34

I have seen a few different methods to add elements to the DOM. The most prevelent seem to be, for example, either

document.getElementById(\'foo\').innerHTM         


        
6条回答
  •  温柔的废话
    2020-11-29 06:06

    First one is straight forward, easier to read, less code and might be faster.
    Second one gives you much more control over the element you create, i.e. makes it much easier to modify the new Element using JS (like attaching events, or, just use it in your code).
    Second way is for "purist" who like "clean" code (no quick and dirty).
    I say, use both, see what fits you better and go with it.

提交回复
热议问题