- 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 APIs
- 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 元素 insertAdjacentText() 方法
insertAdjacentText() 方法允许您在相对于调用此方法的元素的特定位置插入文本内容。
语法
element.insertAdjacentText(position, text);
参数
此方法接受如下所述的两个参数。
| 参数 | 描述 |
|---|---|
| 位置 | 指定相对于元素插入文本内容的位置
|
| 文本 | 要插入的文本字符串。 |
位置选项
- beforebegin: 将内容放在指定元素之前。
- afterbegin: 将内容插入到指定元素内容的起始位置之后。
- beforeend: 将内容插入到指定元素内容的结束位置之前。
- afterend: 将内容添加到指定元素之后。
返回值
此方法直接在指定位置插入文本内容,不返回值。
HTML DOM 元素“insertAdjacentText()”方法示例
下面是一些关于 insertAdjacentText() 方法指定不同元素插入位置的示例。
在 beforebegin 位置插入文本内容
此示例演示了在 HTML DOM 中使用 insertAdjacentText() 方法。它在 id 为 example1 的现有 <p> 元素的起始位置之前插入一个新的带有文本“Text beforebegin”的 <p> 元素。
<!DOCTYPE html>
<html lang="en">
<head>
<title>insertAdjacentText Example - beforebegin</title>
</head>
<body>
<h1>HTML - DOM Element</h1>
<h2>insertAdjacentText() Method</h2>
<p>Inserts new Text Content 'beforebegin'.</p>
<p id="example1">This is an example element.</p>
<button onclick="insertBeforeBegin()">
Insert beforebegin
</button>
<script>
function insertBeforeBegin() {
let element = document.getElementById('example1');
element.insertAdjacentText('beforebegin',
'Text beforebegin ');
}
</script>
</body>
</html>
在 afterbegin 位置插入文本内容
此示例演示了在 HTML DOM 中使用 insertAdjacentText() 方法。它在 id 为 example2 的现有 <p> 元素的起始位置之后插入一个新的带有文本“Text afterbegin”的 <p> 元素。
<!DOCTYPE html>
<html lang="en">
<head>
<title>insertAdjacentText Example - afterbegin</title>
</head>
<body>
<h1>HTML - DOM Element</h1>
<h2>insertAdjacentText() Method</h2>
<p>Inserts new Text Content 'afterbegin'.</p>
<p id="example2">This is an example element.</p>
<button onclick="insertAfterBegin()">
Insert afterbegin
</button>
<script>
function insertAfterBegin() {
let element = document.getElementById('example2');
element.insertAdjacentText('afterbegin',
'Text afterbegin ');
}
</script>
</body>
</html>
在 beforeend 位置插入文本内容
此示例演示了在 HTML DOM 中使用 insertAdjacentText() 方法。它在 id 为 example3 的现有 <p> 元素的结束位置之前插入一个新的带有文本“Text beforeend”的 <p> 元素。
<!DOCTYPE html>
<html lang="en">
<head>
<title>insertAdjacentText Example - beforeend</title>
</head>
<body>
<h1>HTML - DOM Element</h1>
<h2>insertAdjacentText() Method</h2>
<p>Inserts new Text Content 'beforeend'.</p>
<p id="example3">This is an example element.</p>
<button onclick="insertBeforeEnd()">
Insert beforeend
</button>
<script>
function insertBeforeEnd() {
let element = document.getElementById('example3');
element.insertAdjacentText('beforeend',
' Text beforeend');
}
</script>
</body>
</html>
在 afterend 位置插入文本内容
此示例演示了在 HTML DOM 中使用 insertAdjacentText() 方法。它在 id 为 example4 的现有 <p> 元素的结束位置之后插入一个新的带有文本“Text afterend”的 <p> 元素。
<!DOCTYPE html>
<html lang="en">
<head>
<title>insertAdjacentText Example - afterend</title>
</head>
<body>
<h1>HTML - DOM Element</h1>
<h2>insertAdjacentText() Method</h2>
<p>Inserts new Text Content 'afterend'.</p>
<p id="example4">This is an example element.</p>
<button onclick="insertAfterEnd()">
Insert afterend
</button>
<script>
function insertAfterEnd() {
let element = document.getElementById('example4');
element.insertAdjacentText('afterend',
' Text afterend');
}
</script>
</body>
</html>
支持的浏览器
| 方法 | ![]() |
![]() |
![]() |
![]() |
![]() |
|---|---|---|---|---|---|
| insertAdjacentText() | 是 5.0 | 是 12.0 | 是 8.0 | 是 5.0 | 是 11.50 |
html_dom_element_reference.htm
广告




