How to append data to div using JavaScript?

前端 未结 11 1755
忘了有多久
忘了有多久 2020-11-22 09:39

I\'m using AJAX to append data to div element, where I fill the div from JavaScript, how can I append new data to the div without losing the previous data found in div?

11条回答
  •  粉色の甜心
    2020-11-22 10:06

    The following method is less general than others however it's great when you are sure that your last child node of the div is already a text node. In this way you won't create a new text node using appendData MDN Reference AppendData

    let mydiv = document.getElementById("divId");
    let lastChild = mydiv.lastChild;
    
    if(lastChild && lastChild.nodeType === Node.TEXT_NODE ) //test if there is at least a node and the last is a text node
       lastChild.appendData("YOUR TEXT CONTENT");
    

提交回复
热议问题