문법
.detach()
예를 들어
var peach = $("h1").detach();
h1요소를 제거하고 변수 peach에 저장합니다.
예제 코드
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<title>Home</title>
</head>
<script type="text/javascript">
$(document).ready(function(){
var peach;
$("#a").click(function(){
peach = $("h1").detach();
});
$("#b").click(function(){
$("h2").after(peach);
});
});
</script>
<body>
<h1>Peach</h1>
<h2>Melon</h2>
<button id="a" type="button">잘라내기</button>
<button id="b" type="button">붙여넣기</button>
</body>
</html>
실행 테스트
See the Pen 14 by youngjins (@youngjins) on CodePen.