HTML DOM insertAdjacentElement( ) 方法
htmlweb developmentfront end technology更新于 2025/9/14 5:32:17
HTML DOM insertAdjacentElement() 方法在指定位置插入元素。
语法
以下是语法 −
使用positionString和element参数调用insertAdjacentElement()
node.insertAdjacentElement(“positionString”, element)
位置字符串
此处,“positionString”可以是以下 −
| positionString | 描述 |
|---|---|
| afterbegin | 在节点元素开头后插入元素 |
| afterend | 在节点元素后插入元素 |
| beforebegin | 在节点元素前插入元素 |
| beforeend | 在节点元素结尾前插入元素 |
示例
让我们看一个 InsertAdjacentElement() 方法的示例 −
<!DOCTYPE html>
<html>
<head>
<title>insertAdjacentElement()</title>
<style>
form {
width:70%;
margin: 0 auto;
text-align: center;
}
* {
padding: 2px;
margin:5px;
}
input[type="button"] {
border-radius: 10px;
}
</style>
</head>
<body>
<form>
<fieldset>
<legend>insertAdjacentElement( )</legend>
<h1>Family Tree</h1>
<span id="Father">Father --></span>
<span id="GrandFather">Grand Father --></span>
<span id="Myself">Myself</span>
<input type="button" onclick="rectifyTree()" value="Correct Family Tree">
</fieldset>
</form>
<script>
function rectifyTree() {
var FSpan = document.getElementById("Father");
var GFSpan = document.getElementById("GrandFather");
GFSpan.insertAdjacentElement("afterend", FSpan);
}
</script>
</body>
</html>
输出
这将产生以下输出 −
点击"修改家谱"按钮前 −

点击"修改家谱"按钮后 −

相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

