HTML DOM insertAdjacentElement() 메소드는 지정된 위치에 요소를 삽입합니다.
구문
다음은 구문입니다 -
positionString 및 element의 매개변수를 사용하여 insertAdjacentElement() 호출
node.insertAdjacentElement(“positionString”, element)
위치 문자열
여기, "positionString" 다음과 같을 수 있습니다 -
| 위치 문자열 | 설명 |
|---|---|
| 시작 후 | 노드 요소의 시작 뒤에 요소를 삽입합니다. |
| 후 | 노드 요소 뒤에 요소를 삽입합니다. |
| 시작하기 전에 | 노드 요소 앞에 요소를 삽입합니다. |
| 앞에 | 노드 요소의 끝에 요소를 삽입합니다. |
예시
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> 출력
이것은 다음과 같은 출력을 생성합니다 -
'가계도 수정'을 클릭하기 전에 버튼 -

'가계도 수정'을 클릭한 후 버튼 -
