首页  > 前端开发 > js增加html标签

js增加html标签

前端开发 2024-12-19 3

1. 运用 `innerHTML` 特点直接在现有的元素中刺进HTML内容。2. 运用 `createElement` 和 `appendChild` 办法创立新的元素并增加到DOM中。3. 运用 `insertAdjacentHTML` 办法在指定方位刺进HTML。

```javascript// 办法1: 运用innerHTMLfunction addHtmlUsingInnerHtml { var element = document.getElementById; element.innerHTML = htmlContent;}

// 办法2: 运用createElement和appendChildfunction addHtmlUsingCreateElement { var parentElement = document.getElementById; var newElement = document.createElement; newElement.innerHTML = htmlContent; parentElement.appendChild;}

// 办法3: 运用insertAdjacentHTMLfunction addHtmlUsingInsertAdjacentHTML { var element = document.getElementById; element.insertAdjacentHTML;}

// 示例用法addHtmlUsingInnerHtml;

addHtmlUsingCreateElement;

addHtmlUsingInsertAdjacentHTML;```

```javascript

var newDiv = document.createElement(\


Copyright © 2016-2028零基础教程 Rights Reserved. XML地图