Home >Web Front-end >JS Tutorial >How to Append HTML to Container Elements Without Using InnerHTML?
Appending HTML to Container Elements Without InnerHTML
When attempting to append HTML to a container element, using innerHTML may not be ideal due to its tendency to replace existing content. To avoid this issue, consider using the insertAdjacentHTML() method.
insertAdjacentHTML() offers greater control over the placement of the new HTML. It takes two parameters:
Example usage:
var d1 = document.getElementById('one'); d1.insertAdjacentHTML('beforeend', '<div id="two">two</div>');
This method allows you to append HTML without creating an additional span tag or replacing existing content, providing a more efficient and consistent solution.
The above is the detailed content of How to Append HTML to Container Elements Without Using InnerHTML?. For more information, please follow other related articles on the PHP Chinese website!