js dom innerhtml
时间: 2023-10-01 07:10:20 浏览: 37
The innerHTML property in JavaScript is used to get or set the HTML content of an element. It can be used to insert or replace content within an element.
Here is an example of how to use innerHTML to change the text of a paragraph element:
HTML:
```html
<p id="myParagraph">Hello World!</p>
```
JavaScript:
```javascript
// Get the element with id "myParagraph"
var paragraph = document.getElementById("myParagraph");
// Change the text of the paragraph using innerHTML
paragraph.innerHTML = "Goodbye World!";
```
After running this code, the paragraph element will now display "Goodbye World!" instead of "Hello World!".
It's important to note that using innerHTML can potentially introduce security vulnerabilities if the content being inserted is not properly sanitized. It's recommended to use alternative methods such as textContent or createTextNode when dealing with user-generated content.