본문 바로가기

JavaScript

Node interface

#Attribute

속성을 사용할땐 노드명.속성명 으로 사용한다

parentNode : 현재 노드의 부모의 참조값

childNodes : 현재 노드의 자식들을 NodeList로 가지고 있음.

firstChild : 첫번째 자식노드의 참조값

lastChild : 마지막 자식노드의 참조값

previousSibling : 현재 노드 이전의 형제의 참조값

nextSibling : 현재 노드 다음에 있는 형제의 참조값

 

# Method

1. appendChild(childNode)

: 자식 노드를 추가하는 메소드. 매개변수로 받은 노드를 자식 노드로 추가한다. 이미 자식노드가 있다면 순서대로 추가된다.

appendChild modified in DOM Level 3
Adds the node newChild to the end of the list of children of this node. If the newChild is already in the tree, it is first removed.
Parameters
newChild of type Node
The node to add.
If it is a DocumentFragment object, the entire contents of the document fragment are moved into the child list of this node
Return Value

Node

The node added.

Exceptions

DOMException

HIERARCHY_REQUEST_ERR: Raised if this node is of a type that does not allow children of the type of the newChild node, or if the node to append is one of this node's ancestors or this node itself, or if this node is of type Document and the DOM application attempts to append a second DocumentType or Element node.

WRONG_DOCUMENT_ERR: Raised if newChild was created from a different document than the one that created this node.

NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly or if the previous parent of the node being inserted is readonly.

NOT_SUPPORTED_ERR: if the newChild node is a child of the Document node, this exception might be raised if the DOM implementation doesn't support the removal of the DocumentType child or Element child.

 

2. insertBefore(newNode, refNode)

: 기존의 노드 앞에 새로운 노드를 추가한다. 두번째 매개변수로 받은 자식 노드 앞에 첫번째 매개변수로 받은 새로운 자식 노드를 추가한다.

insertBefore modified in DOM Level 3
Inserts the node newChild before the existing child node refChild. If refChild is null, insert newChild at the end of the list of children.
If newChild is a DocumentFragment object, all of its children are inserted, in the same order, before refChild. If the newChild is already in the tree, it is first removed.

Note: Inserting a node before itself is implementation dependent.

Parameters
newChild of type Node
The node to insert.
refChild of type Node
The reference node, i.e., the node before which the new node must be inserted.
Return Value

Node

The node being inserted.

Exceptions

DOMException

HIERARCHY_REQUEST_ERR: Raised if this node is of a type that does not allow children of the type of the newChild node, or if the node to insert is one of this node's ancestors or this node itself, or if this node is of type Document and the DOM application attempts to insert a second DocumentType or Element node.

WRONG_DOCUMENT_ERR: Raised if newChild was created from a different document than the one that created this node.

NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly or if the parent of the node being inserted is readonly.

NOT_FOUND_ERR: Raised if refChild is not a child of this node.

NOT_SUPPORTED_ERR: if this node is of type Document, this exception might be raised if the DOM implementation doesn't support the insertion of a DocumentType or Element node.

 

3. removeChild(childNode)

: 매개변수로 받은 자식노드를 삭제하고 삭제한 자식 노드를 반환한다.

removeChild modified in DOM Level 3
Removes the child node indicated by oldChild from the list of children, and returns it.
Parameters
oldChild of type Node
The node being removed.
Return Value

Node

The node removed.

Exceptions

DOMException

NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.

NOT_FOUND_ERR: Raised if oldChild is not a child of this node.

NOT_SUPPORTED_ERR: if this node is of type Document, this exception might be raised if the DOM implementation doesn't support the removal of the DocumentType child or the Element child.

4. replaceChild(newChild, oldChild)

: 기존의 자식노드를 새로운 자식노드로 바꾸는 함수. 교체된 노드가 반환된다.

replaceChild modified in DOM Level 3
Replaces the child node oldChild with newChild in the list of children, and returns the oldChild node.
If newChild is a DocumentFragment object, oldChild is replaced by all of the DocumentFragment children, which are inserted in the same order. If the newChild is already in the tree, it is first removed.

Note: Replacing a node with itself is implementation dependent.

Parameters
newChild of type Node
The new node to put in the child list.
oldChild of type Node
The node being replaced in the list.
Return Value

Node

The node replaced.

Exceptions

DOMException

HIERARCHY_REQUEST_ERR: Raised if this node is of a type that does not allow children of the type of the newChild node, or if the node to put in is one of this node's ancestors or this node itself, or if this node is of type Document and the result of the replacement operation would add a second DocumentType or Element on the Document node.

WRONG_DOCUMENT_ERR: Raised if newChild was created from a different document than the one that created this node.

NO_MODIFICATION_ALLOWED_ERR: Raised if this node or the parent of the new node is readonly.

NOT_FOUND_ERR: Raised if oldChild is not a child of this node.

NOT_SUPPORTED_ERR: if this node is of type Document, this exception might be raised if the DOM implementation doesn't support the replacement of the DocumentType child or Element child.

 

'JavaScript' 카테고리의 다른 글

JSON  (0) 2016.04.08
HTML DOM 함수 - document interface  (0) 2016.04.06
DOM  (0) 2016.04.06
내장 객체 - String  (0) 2016.04.05
Array  (0) 2016.04.04