如何使用 jQuery 克隆元素?
要使用 jQuery 克隆元素,請使用 jQuery.clone() 方法。 clone() 方法克隆匹配的 DOM 元素並選擇克隆元素。
這對於將元素的副本移動到 DOM 中的另一個位置非常有用。
示例
你可以嘗試執行以下程式碼來了解如何使用 jQuery 克隆元素
<html>
<head>
<title>jQuery clone() method</title>
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("div").click(function () {
$(this).clone().insertAfter(this);
});
});
</script>
<style>
.div {
margin:10px;
padding:12px;
border:2px solid #666;
width:60px;
}
</style>
</head>
<body>
<p>Click on any square below to see the result:</p>
<div class = "div" style = "background-color:blue;"></div>
<div class = "div" style = "background-color:green;"></div>
<div class = "div" style = "background-color:red;"></div>
</body>
</html>
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP