답변:
clone()요소의 전체 복사본을 얻으려면 메서드 를 사용하고 싶을 것입니다 .
$(function(){
var $button = $('.button').clone();
$('.package').html($button);
});
전체 데모 : http://jsfiddle.net/3rXjx/
로부터 의 jQuery 문서 :
.clone () 메서드는 일치하는 요소 집합의 전체 복사를 수행합니다. 즉, 일치하는 요소와 모든 하위 요소 및 텍스트 노드를 복사합니다. 삽입 메소드 중 하나와 함께 사용하면 .clone ()은 페이지에서 요소를 복제하는 편리한 방법입니다.
$에서 var $button. 나는 js를 믿고 var를 간단히 선언 할 수 있습니다 var button.
clone 및 appendTo 함수를 사용하여 코드 복사 :
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
<div id="copy"><a href="http://brightwaay.com">Here</a> </div>
<br/>
<div id="copied"></div>
<script type="text/javascript">
$(function(){
$('#copy').clone().appendTo('#copied');
});
</script>
</body>
</html>
$(document).ready(function(){
$("#btn_clone").click(function(){
$("#a_clone").clone().appendTo("#b_clone");
});
});
.container{
padding: 15px;
border: 12px solid #23384E;
background: #28BAA2;
margin-top: 10px;
}
<!DOCTYPE html>
<html>
<head>
<title>jQuery Clone Method</title>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<body>
<div class="container">
<p id="a_clone"><b> This is simple example of clone method.</b></p>
<p id="b_clone"><b>Note:</b>Click The Below button Click Me</p>
<button id="btn_clone">Click Me!</button>
</div>
</body>
</html>