begin: 20170702
version: 20170724
用jQuery clone()方法可以賦值一個節(jié)點,但是如果這個節(jié)點帶有id屬性沪铭,那么可能導致該id在同一個頁面中被重復使用。比如我們在head>script:nth-of-type(3)中代碼的最后一行復制了#target5元素脖镀。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
<link rel="stylesheet"
>
<script src="program.js"></script>
<script src="http://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>
<script>
$(document).ready(function() {
$("#target1").css("color", "red");
$("#target1").prop("disabled", true);
$("#target4").remove();
$("#target2").appendTo("#right-well");
$("#target5").clone().appendTo("#left-well");
});
</script>
</head>
<body>
<!-- 請只修改這條注釋以上的代碼 -->
<div class="container-fluid">
<h3 class="text-primary text-center">jQuery Playground</h3>
<div class="row">
<div class="col-xs-6">
<h4>#left-well</h4>
<div class="well" id="left-well">
<button class="btn btn-default target" id="target1">#target1</button>
<button class="btn btn-default target" id="target2">#target2</button>
<button class="btn btn-default target" id="target3">#target3</button>
</div>
</div>
<div class="col-xs-6">
<h4>#right-well</h4>
<div class="well" id="right-well">
<button class="btn btn-default target" id="target4">#target4</button>
<button class="btn btn-default target" id="target5">#target5</button>
<button class="btn btn-default target" id="target6">#target6</button>
</div>
</div>
</div>
</div>
</body>
</html>
結(jié)果發(fā)現(xiàn)出現(xiàn)了兩個id為target5
的元素瓦堵,這一點需要注意。