
- HTML 教程
- HTML - 首頁
- HTML - 路線圖
- HTML - 簡介
- HTML - 歷史與演變
- HTML - 編輯器
- HTML - 基本標籤
- HTML - 元素
- HTML - 屬性
- HTML - 標題
- HTML - 段落
- HTML - 字型
- HTML - 塊
- HTML - 樣式表
- HTML - 格式化
- HTML - 引號
- HTML - 註釋
- HTML - 顏色
- HTML - 圖片
- HTML - 圖片地圖
- HTML - 內嵌框架
- HTML - 短語元素
- HTML - 元標籤
- HTML - 類
- HTML - ID
- HTML - 背景
- HTML 表格
- HTML - 表格
- HTML - 表格頭和標題
- HTML - 表格樣式
- HTML - 表格 Colgroup
- HTML - 巢狀表格
- HTML 列表
- HTML - 列表
- HTML - 無序列表
- HTML - 有序列表
- HTML - 定義列表
- HTML 連結
- HTML - 文字連結
- HTML - 圖片連結
- HTML - 郵件連結
- HTML 顏色名稱和值
- HTML - 顏色名稱
- HTML - RGB
- HTML - HEX
- HTML - HSL
- HTML 表單
- HTML - 表單
- HTML - 表單屬性
- HTML - 表單控制元件
- HTML - 輸入屬性
- HTML 媒體
- HTML - 影片元素
- HTML - 音訊元素
- HTML - 嵌入多媒體
- HTML 頁首
- HTML - 頭元素
- HTML - 新增 Favicon
- HTML - Javascript
- HTML 佈局
- HTML - 佈局
- HTML - 佈局元素
- HTML - 使用 CSS 進行佈局
- HTML - 響應式
- HTML - 符號
- HTML - 表情符號
- HTML - 樣式指南
- HTML 圖形
- HTML - SVG
- HTML - Canvas
- HTML API
- HTML - Geolocation API
- HTML - 拖放 API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web 儲存
- HTML - 伺服器傳送事件
- HTML 雜項
- HTML - 文件物件模型 (DOM)
- HTML - MathML
- HTML - 微資料
- HTML - IndexedDB
- HTML - Web 訊息傳遞
- HTML - Web CORS
- HTML - Web RTC
- HTML 演示
- HTML - 音訊播放器
- HTML - 影片播放器
- HTML - 網頁幻燈片
- HTML 工具
- HTML - Velocity Draw
- HTML - 二維碼
- HTML - Modernizer
- HTML - 驗證
- HTML - 顏色拾取器
- HTML 參考
- HTML - 速查表
- HTML - 標籤參考
- HTML - 屬性參考
- HTML - 事件參考
- HTML - 字型參考
- HTML - ASCII 碼
- ASCII 碼錶查詢
- HTML - 顏色名稱
- HTML - 實體
- MIME 媒體型別
- HTML - URL 編碼
- 語言 ISO 程式碼
- HTML - 字元編碼
- HTML - 已棄用標籤
- HTML 資源
- HTML - 快速指南
- HTML - 有用資源
- HTML - 顏色程式碼生成器
- HTML - 線上編輯器
HTML - DOM 元素 insertAdjacentElement() 方法
**insertAdjacentElement()** 方法允許您在網頁上將新的 HTML 元素精確地插入到另一個元素的相對位置。
語法
element.insertAdjacentElement(position, elementToInsert);
引數
引數 | 描述 |
---|---|
position | 指定相對於元素插入 HTML 內容的位置 beforebegin afterbegin beforeend afterend |
elementToInsert | 要插入結構中的新元素。 |
位置選項
- **beforebegin:** 放置在指定元素之前。
- **afterbegin:** 插入到指定元素內容的開頭之後。
- **beforeend:** 插入到指定元素內容的結尾之前。
- **afterend:** 新增到指定元素之後。
返回值
此方法直接在指定位置插入,不返回值。
HTML DOM 元素“insertAdjacentElement()”方法示例
以下是一些關於 insertAdjacentElement() 方法如何指定不同元素插入位置的示例。
在 beforebegin 處插入新內容
此示例演示了在 HTML DOM 中使用 insertAdjacentElement 方法。它在現有的具有 id 為 container 的 **<div>** 元素之前插入一個新的 **<p>** 元素,其中包含文字“New content-beforebegin”。
<!DOCTYPE html> <html lang="en"> <head> <style> .container { border: 1px solid #ccc; padding: 10px; margin: 10px; } </style> </head> <body> <h1>HTML DOM Element</h1> <h2>insertAdjacentElement() Method</h2> <p>The container starts the existing content.</p> <p>Adds content before the existing element....</p> <div id="container" class="container"> <h2 id="sectionLabel">Before:</h2> <p>This is the existing content.</p> </div> <button onclick="addNewElement()"> Add New Element Before Beginning </button> <script> function addNewElement() { let newElement = document.createElement('p'); newElement.textContent ='New content - beforebegin'; newElement.style.backgroundColor = 'lightgreen'; document.getElementById ('container').insertAdjacentElement ('beforebegin', newElement); document.getElementById('sectionLabel').textContent= 'After:'; } </script> </body> </html>
在 afterbegin 處插入新內容
此示例演示了在 HTML DOM 中使用 insertAdjacentElement 方法。它在現有的具有 id 為 container 的 <div> 元素的開頭之後插入一個新的 <p> 元素,其中包含文字“New content-afterbegin”。
<!DOCTYPE html> <html lang="en"> <head> <style> .container { border: 1px solid #ccc; padding: 10px; margin: 10px; } </style> </head> <body> <h1>HTML DOM Element</h1> <h2>insertAdjacentElement() Method</h2> <p>The container(box) starts the existing content.</p> <p>Adds content after the existing element....</p> <div id="container" class="container"> <h3 id="sectionLabel">Before:</h3> <p>This is the existing content.</p> </div> <button onclick="addNewElement()"> Add New Element After Beginning </button> <script> function addNewElement() { let newElement = document.createElement('p'); newElement.textContent = 'New content - afterbegin'; newElement.style.backgroundColor = 'lightblue'; document.getElementById('container').insertBefore (newElement,document.querySelector ('#container > h3')); document.getElementById('sectionLabel').textContent= 'After:'; } </script> </body> </html>
在 beforeend 處插入新內容
此示例演示了在 HTML DOM 中使用 insertAdjacentElement 方法。它在現有的具有 id 為 myContainer 的 <div> 元素的結尾之前插入一個新的 <p> 元素,其中包含文字“New content-beforeend”。
<!DOCTYPE html> <html lang="en"> <head> <style> .container { border: 1px solid #ccc; padding: 10px; margin: 10px; } </style> </head> <body> <h1>HTML DOM Element</h1> <h2>insertAdjacentElement() Method</h2> <p>Container marks the content's end..</p> <p>Adds content before the existing element....</p> <div class="container" id="myContainer"> <h3>Before:</h3> <p>This is the existing content.</p> </div> <button onclick="addNewElement()"> Add New Element Before End </button> <script> function addNewElement() { let newElement = document.createElement('p'); newElement.textContent = 'New content beforeend'; newElement.style.backgroundColor = 'lightblue'; // Insert the new element before the existing one let container = document.getElementById ('myContainer'); container.insertAdjacentElement ('beforeend', newElement); } </script> </body> </html>
在 afterend 處插入新內容
此示例演示了在 HTML DOM 中使用 insertAdjacentElement() 方法。它在現有的具有 id 為 container 的 <div> 元素的結尾之後插入一個新的 <p> 元素,其中包含文字“New content-afterend”。
<!DOCTYPE html> <html lang="en"> <head> <style> .container { border: 1px solid #ccc; padding: 10px; margin: 10px; } </style> </head> <body> <h1>HTML DOM Element</h1> <h2>insertAdjacentElement() Method</h2> <p>Container marks the content's end..</p> <p>Adds new content after the existing one...</p> <div id="container" class="container"> <h3 id="sectionLabel">Before:</h3> <p>This is the existing content.</p> </div> <button onclick="addNewElement()"> Add New Element After End </button> <script> function addNewElement() { let newElement = document.createElement('p'); newElement.textContent = 'New content - afterend'; newElement.style.backgroundColor = 'lightcoral'; // Insert new element after the existing one let existingElement = document.getElementById('container'); existingElement.insertAdjacentElement ('afterend', newElement); let sectionLabel = document.getElementById('sectionLabel'); sectionLabel.textContent = 'After:'; } </script> </body> </html>
支援的瀏覽器
方法 | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
insertAdjacentElement() | 是 5.0 | 是 12.0 | 是 8.0 | 是 5.0 | 是 11.50 |
html_dom_element_reference.htm
廣告