在 jQuery 中,jQuery.prepend() 和 jQuery.prependTo() 方法有什麼區別?
jQuery.prepend()
prepend( content ) 方法將內容新增到每個匹配元素的內部開頭。以下是此方法使用所有引數的描述:
- content − 要在每個目標之後插入的內容。這可以是 HTML 或文字內容
示例
您可以嘗試執行以下程式碼來學習如何在 jQuery 中使用 prepend() 方法:
<html>
<head>
<title>jQuery prepend() 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).prepend('<div class = "div"></div>' );
});
});
</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>jQuery.prependTo()
prependTo(selector) 方法將所有匹配的元素新增到另一個指定的元素集中。
以下是此方法使用所有引數的描述:
- selector − 這是將內容新增到其開頭的目標。
示例
您可以嘗試執行以下程式碼來學習如何使用 prependTo() 方法:
<html>
<head>
<title>jQuery prependTo() method</title>
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("div").click(function () {
$(this).prependTo("#result");
});
});
</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>
<p id = "result"> THIS IS TEST </p>
<hr />
<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