Adding new elements into DOM using JavaScript (appendChild)

后端 未结 4 1564
[愿得一人]
[愿得一人] 2021-01-05 04:03

I sometimes need to add elements (such as a new link and image) to an existing HTML page, but I only have access to a small portion of the page far from where I need to inse

4条回答
  •  执念已碎
    2021-01-05 04:50

    If you're not adding many things, the way you've been doing it is ideal vs innerHTML. If you're doing it frequently though, you might just create a generic function/object that takes the pertinent information as parameters and does the dirty work. IE

    function addImage(src,width,height,alt,appendElem,href) {...}
    

    I do this often in my own projects using prototyping to save time.

提交回复
热议问题