- HTML 教程
- HTML - 首页
- HTML - 路线图
- HTML - 简介
- HTML - 历史与演变
- HTML - 编辑器
- HTML - 基本标签
- HTML - 元素
- HTML - 属性
- HTML - 标题
- HTML - 段落
- HTML - 字体
- HTML - 块
- HTML - 样式表
- HTML - 格式化
- HTML - 引用
- HTML - 注释
- HTML - 颜色
- HTML - 图片
- HTML - 图片地图
- HTML - Iframes
- 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 - Head 元素
- 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 元素 insertAdjacentHTML() 方法
**insertAdjacentHTML()** 方法允许您在相对于调用此方法的元素的特定位置插入指定的 HTML 代码。
语法
element.insertAdjacentHTML(position, html);
参数
| 参数 | 描述 |
|---|---|
| position | 指定相对于元素插入 HTML 内容的位置 beforebegin afterbegin beforeend afterend |
| html | 要插入的 HTML 字符串。 |
位置选项
- **beforebegin:** 将内容放置在指定元素之前。
- **afterbegin:** 将内容插入到指定元素内容的开头之后。
- **beforeend:** 将内容插入到指定元素内容的结尾之前。
- **afterend:** 将内容添加到指定元素之后。
返回值
此方法直接在指定位置插入内容,不返回值。
HTML DOM 元素“insertAdjacentHTML()”方法示例
以下是一些关于 insertAdjacentHTML() 方法如何指定不同元素插入位置的示例。
在 afterend 插入 HTML 代码
此示例演示了在 HTML DOM 中使用 insertAdjacentHTML() 方法。它在 id 为 container 的现有 <div> 元素的结尾之后插入一个新的 <p> 元素,其中包含文本“New content-afterend”。
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.container {
border: 1px solid #ccc;
padding: 10px;
margin: 10px;
}
.new-content {
background-color: #d6ffff;
}
</style>
</head>
<body>
<h1>HTML - DOM Element</h1>
<h2>insertAdjacentHTML() Method</h2>
<p>Inserts new HTML content 'afterend'.</p>
<div id="container" class="container">
<h4>Existing content within this container.<h4>
</div>
<button onclick="insertAfterEnd()">
Insert afterend
</button>
<script>
function insertAfterEnd() {
let container = document.getElementById('container');
container.insertAdjacentHTML
('afterend',
'<div class="new-content">New content-afterend</div>');
}
</script>
</body>
</html>
在 beforeend 插入 HTML 代码
此示例演示了在 HTML DOM 中使用 insertAdjacentHTML() 方法。它在 id 为 myContainer 的现有 <div> 元素的结尾之前插入一个新的 <p> 元素,其中包含文本“New content-beforeend”。
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.container {
border: 1px solid #ccc;
padding: 10px;
margin: 10px;
}
.new-content {
background-color: #b3e0ff;
}
</style>
</head>
<body>
<h1>HTML - DOM Element</h1>
<h2>insertAdjacentHTML() Method</h2>
<p>Inserts new HTML content 'beforeend'.</p>
<div id="container" class="container">
<h4>Existing content within this container.<h4>
</div>
<button onclick="insertBeforeEnd()">
Insert beforeend
</button>
<script>
function insertBeforeEnd() {
let container = document.getElementById('container');
container.insertAdjacentHTML('beforeend',
'<div class="new-content">New content-beforeend</div>'
);
}
</script>
</body>
</html>
在 afterbegin 插入 HTML 代码
此示例演示了在 HTML DOM 中使用 insertAdjacentHTML() 方法。它在 id 为 container 的现有 <div> 元素的开头之后插入一个新的 <p> 元素,其中包含文本“New content-afterbegin”。
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.container {
border: 1px solid #ccc;
padding: 10px;
margin: 10px;
}
.new-content {
background-color: #99ff99;
}
</style>
</head>
<body>
<h1>HTML - DOM Element</h1>
<h2>insertAdjacentHTML() Method</h2>
<p>Inserts new HTML content 'beforebegin'.</p>
<div id="container" class="container">
<h4>Existing content within this container.<h4>
</div>
<button onclick="insertAfterBegin()">
Insert afterbegin
</button>
<script>
function insertAfterBegin() {
let container = document.getElementById('container');
container.insertAdjacentHTML('afterbegin',
'<div class="new-content">New content-afterbegin</div>'
);
}
</script>
</body>
</html>
在 beforebegin 插入 HTML 代码
此示例演示了在 HTML DOM 中使用 insertAdjacentHTML() 方法。它在 id 为 container 的现有 <div> 元素之前插入一个新的 <p> 元素,其中包含文本“New content-beforebegin”。
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.container {
border: 1px solid #ccc;
padding: 10px;
margin: 10px;
}
.new-content {
background-color: #ff8080;
}
</style>
</head>
<body>
<h1>HTML - DOM Element</h1>
<h2>insertAdjacentHTML() Method</h2>
<p>Inserts new HTML content 'beforebegin'.</p>
<div id="container" class="container">
<h4>Existing content within this container.<h4>
</div>
<button onclick="insertBeforeBegin()">
Insert beforebegin
</button>
<script>
function insertBeforeBegin() {
let container = document.getElementById('container');
container.insertAdjacentHTML('beforebegin',
'<div class="new-content">New content-beforebegin</div>'
);
}
</script>
</body>
</html>
支持的浏览器
| 方法 | ![]() |
![]() |
![]() |
![]() |
![]() |
|---|---|---|---|---|---|
| insertAdjacentHTML() | 是 5.0 | 是 12.0 | 是 8.0 | 是 5.0 | 是 11.50 |
html_dom_element_reference.htm
广告




