如何使用 jQuery 在每個匹配的元素後插入內容?
若要使用 jQuery 在每個匹配的元素後插入內容,請使用 after() 方法。
after( content ) 方法在每個匹配的元素後插入內容。以下是該方法使用所有引數的說明 -
- 內容 - 插入到每個目標後方的內容。這可以是 HTML 或文字內容
示例
您可以嘗試執行以下程式碼來了解如何在每個匹配的元素後插入內容
<html>
<head>
<title>jQuery after() 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).after('<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>
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP