clone() 함수를 이용해서 복사하기
clone() 함수를 사용하면 아주 쉽게 element 를 복사할 수 있다.
1 2 3 4 5 6 7 8 9 | <div class = "title" ></div> <h2>This is title</h2> <script> $( document ).ready( function () { var title = $( 'h2' ). clone (); $( '.title' ).html(title); }); </script> |
결과
1 2 | <div class = "title" ><h2>This is title</h2></div> <h2>This is title</h2> |
clone() 함수를 사용하지 않으면?
clone() 함수를 사용하지 않으면 복사가 되는 것이 아니고 element가 이동을 하게 된다.
1 2 3 4 5 6 7 8 9 | <div class = "title" ></div> <h2>This is title</h2> <script> $( document ).ready( function () { var title = $( 'h2' ); $( '.title' ).html(title); }); </script> |
결과
<div class="title"><h2>This is title</h2></div>
'Javascript > jQuery' 카테고리의 다른 글
[jQuery 함수] has() 특정 element를 자식으로 가지고 있는 dom element 선택하기 (0) | 2015.07.27 |
---|---|
[jQuery 함수] $.trim() 문자열 공백 제거하기 (0) | 2015.07.27 |
[jQuery 함수] after(), before() 이용해서 DOM 위치 이동시키기 (0) | 2015.07.27 |
jquery 비 동기 통신 $.ajax(), $.get(), $.post() 사용방법 (0) | 2015.07.07 |
[jQuery 함수] not() 특정 Element 를 제외한 나머지 Element 선택하기 (0) | 2015.07.03 |