JavaScript

[DOM_Element] JS - cloneNode() 메서드 - 노드복사

cloneNode() 예제

 

<ul id="hz"><li>홈짱닷컴</li><li>Homzzang.com</li></ul>

<ul id="code"><li>HTML</li><li>CSS</li></ul>


<button onclick="homzzang()">클릭</button>


<script>

function homzzang() {

  var item = document.getElementById("code").lastChild;

  var copy = item.cloneNode(true);

  document.getElementById("hz").appendChild(copy);

}

</script>

 

결과보기

 

 

주의: 아래처럼 리스트 관련 코드가 줄바꿈 되어 있는 경우, 정상작동 안 함.

 

<ul id="hz">

  <li>홈짱닷컴</li>

  <li>Homzzang.com</li>  

</ul>

<ul id="code">

  <li>HTML</li>

  <li>CSS</li>  

</ul>


<button onclick="homzzang()">클릭</button>


<script>

function homzzang() {

  var item = document.getElementById("code").lastChild;

  var copy = item.cloneNode(true);

  document.getElementById("hz").appendChild(copy);

}

</script>

 

결과보기

 

 

에러 원인: 

 

1.

요소 안 공백은 text로 간주되며, text는 노드로 간주됨.

 

2.

첫 번째 <li> 앞에 공백 존재 시, 결과값은 "undefined" 나옴.


 

 


분류 제목
Window_Object JS - window 객체 - 윈도우객체 (속성/메서드) 종류
API_Storage JS - Storage Object - 스토리지 객체 (※ 데이터 저장소)
Object JS - Object Constructor - 객체생성자종류 ★
Object JS - Object Prototypes - 객체프로토타입 (= 객체원형) ★
Object JS - object ECMAScript 5 - 객체혁명
DOM_Attribute JS - attr.isId -
DOM_Attribute JS - attr.name -
DOM_Attribute JS - attr.value -
DOM_Attribute JS - attr.specified -
DOM_Attribute JS - nodemap.getNamedItem() 메서드 -
DOM_Attribute JS - nodemap.item() 메서드 -
DOM_Attribute JS - nodemap.length -
DOM_Attribute JS - nodemap.removeNamedItem() 메서드 -
DOM_Attribute JS - nodemap.setNamedItem() 메서드 -
Window_Console JS - console.assert() 메서드 - 표현식이 FALSE인지 확인 (= 표현식 거짓 여부 체크 …
30/89
목록
 홈  PC버전 로그인 일본어
그누앞단언어
그누뒷단언어
그외코딩언어
그누보드
제작의뢰
Q&A
커뮤니티
웹유틸
회원센터
홈짱닷컴 PC버전 로그인