Should you add HTML to the DOM using innerHTML or by creating new elements one by one?

前端 未结 3 1747
暖寄归人
暖寄归人 2020-12-08 16:17

There are two methods to add HTML-code to the DOM and I don\'t know what is the best way to do it.

First method

The first way is the easy on

3条回答
  •  半阙折子戏
    2020-12-08 17:05

    If I am going to re-use the div later in the code, I'll build it and put it in a var, usually with a $ prefix so I know it's a jQuery object. If it's a one-off thing I'll just do a:

     $('body').append(the stuff)
    

提交回复
热议问题