Home >Web Front-end >JS Tutorial >How to Append HTML to the DOM Without Using `div.innerHTML`?
When attempting to append a HTML string to the DOM, the typical approach of div.innerHTML = str may seem convenient, but it is often discouraged due to potential security implications. This question explores an alternative solution for appending HTML strings to a specific element.
In this scenario, the task is to append the HTML string
Just some text here
to theHere's how it's done:
div.insertAdjacentHTML('beforeend', str);
The insertAdjacentHTML method takes two parameters:
Using div.insertAdjacentHTML('beforeend', str) ensures that the HTML string is appended inside the
A live demonstration of this solution can be found here: http://jsfiddle.net/euQ5n/
The above is the detailed content of How to Append HTML to the DOM Without Using `div.innerHTML`?. For more information, please follow other related articles on the PHP Chinese website!