How to append text inside a div element?
P粉567112391
P粉567112391 2023-08-21 11:31:02
0
2
485
<p>I am using AJAX to append data to a <code><div></code> element, which I populate via JavaScript. How do I append new data to <code><div></code> without losing the previous data within it? </p>
P粉567112391
P粉567112391

reply all(2)
P粉087951442

Use appendChild:

var theDiv = document.getElementById("<ID_OF_THE_DIV>");
var content = document.createTextNode("<YOUR_CONTENT>");
theDiv.appendChild(content);

Use innerHTML:
As @BiAiB reminds me, this method will remove all listeners for existing elements. So if you plan to use this version, please proceed with caution.

var theDiv = document.getElementById("<ID_OF_THE_DIV>");
theDiv.innerHTML += "<YOUR_CONTENT>";
P粉190443691

Try this:

var div = document.getElementById('divID');

div.innerHTML += '额外的内容';
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template