일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
- addEventListener
- CSS
- modal
- a11y
- Dom
- textContent
- ES5
- innerText
- HTML
- react
- keyup
- es6
- Event
- javascript
- yet
- Review
- Temporal dead zone
- UI
- node.js
- beforeinput
- css:position
- node
- Empty
- keyboardEvent
- nodeValue
- js
- for loop
- innerHTML
- TypingEffect
- dotenv
- Today
- Total
the murmurous sea
HTML DOM: insertBefore() 본문
1. method
2. inserts a node before a reference node as a child of a specified parent node.
3. use the insertBefore method to insert/move an existing element.
3. a node cannot be in two locations of the document simultaneously.
: If the given node already exists in the document, insertBefore() moves it from its current position to the new position.
(That is, it will automatically be removed from its existing parent before appending it to the specified new parent.)
4. If the given child is a DocumentFragment, the entire contents of the DocumentFragment are moved into the child list.
[Syntax]
let insertedNode = parentNode.insertBefore(newNode, referenceNode)
insertedNode | The node being inserted (the same as newNode) | |
parentNode | The parent of the newly inserted node. | |
newNode | Required | The node object you want to insert. |
referenceNode | Required |
The child node you want to insert the new node before. |
If this is null, then newNode is inserted at the end of parentNode's child nodes. | ||
It's not an optional parameter. Must explicitly pass a Node or null. |
[Return value]
The added child A Node Object, representing the inserted node.
: unless newNode is a DocumentFragment, in which case the empty DocumentFragment is returned.
[insertAfter()]
There is no insertAfter() method. It can be emulated by combining the insertBefore method with Node.nextSibling.
In this example, sp1 could be inserted after sp2 using:
parentDiv.insertBefore(sp1, sp2.nextSibling)
If sp2 does not have a next sibling, then it must be the last child.
: sp2.nextSibling returns null, and sp1 is inserted at the end of the child node list (immediately after sp2).
https://developer.mozilla.org/en-US/docs/Web/API/Node/insertBefore
https://www.w3schools.com/jsref/met_node_insertbefore.asp
'#dev > 개념정리' 카테고리의 다른 글
JS: padStart() & padEnd() (0) | 2020.05.04 |
---|---|
[HTML DOM] DocumentFragment (0) | 2020.05.02 |
선언적 함수 vs 함수표현식 vs 익명함수 (waiting list) (0) | 2020.05.02 |
JS DOM: how to insert a new node AFTER an existing node (0) | 2020.05.01 |
HTML, JS: DOM (0) | 2020.05.01 |