jQuery 中的 jQuery add() 和 jQuery append() 方法有哪些區別?
jQuery add() 方法
如果你想向一組已經存在的元素新增元素,那麼可以使用 add() 方法。
示例
你可以執行以下程式碼來學習如何使用 jQuery.add() 方法
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("h1").add("span").css("background-color", "yellow");
});
</script>
</head>
<body>
<h1>Tutorialspoint</h1>
<p>Text Tutorials for free.</p>
<span>Video Tutorials for free.</span>
</body>
</html>jQuery append() 方法
append() 方法將內容附加到每個匹配元素的內部。
示例
你可以執行以下程式碼來學習如何使用 jQuery append() 方法
<html>
<head>
<title>jQuery append() 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).append('<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>
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP