Node.cloneNode:對Node節(jié)點進行克隆藐握,分為深克隆和淺克隆翰萨。
示例代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box {
width: 100%;
height: 300px;
border: 2px solid pink;
display: flex;
}
.part1,
.part2 {
flex: 1;
border: 1px solid green;
margin: 10px;
text-align: center;
line-height: 300px;
font-size: 40px;
color: skyblue;
}
</style>
</head>
<body>
<div class="box" id="box">
<div class="part1">
part1
</div>
<div class="part2">
part2
</div>
</div>
<script>
const el = document.getElementById('box')
const clone_deep = el.cloneNode(true)
const clone_shallow = el.cloneNode(false)
console.log('深克隆:', clone_deep, '淺克隆:', clone_shallow)
</script>
</body>
</html>
截圖