clone() 함수를 이용해서 복사하기

clone() 함수를 사용하면 아주 쉽게 element 를 복사할 수 있다.

<div class="title"></div>
<h2>This is title</h2>

<script>
$( document ).ready( function() {
    var title = $('h2').clone();
    $('.title').html(title);
});
</script>

결과

<div class="title"><h2>This is title</h2></div>
<h2>This is title</h2>

clone() 함수를 사용하지 않으면?

clone() 함수를 사용하지 않으면 복사가 되는 것이 아니고 element가 이동을 하게 된다.

<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>