Best way to create large static DOM elements in JavaScript?

前端 未结 5 1534
野性不改
野性不改 2020-12-23 10:13

I have many elements that one of my JS widgets needs to create and add to the DOM often. They never change.

So one option would be to store the HTML itself as a stri

5条回答
  •  北海茫月
    2020-12-23 10:25

    You can try doing an AJAX fetch of the static HTML block instead of storing it in the page itself. It allows you to be more flexible with what kind of block you want to insert in the future as well.

    Alternatively (this is just a random idea which and not very well fleshed out), you can store the "structure" as JSON data and then dynamically parse it. Possibly something like {"div": {"div": {"span": "Text here"}}} for

    Text here
    . I'd still go with AJAX though. :)

提交回复
热议问题