如何將前一個元素新增到當前 jQuery 選擇中?
要將前一個元素新增到當前 jQuery 選擇中,請使用 insertBefore() 方法。
示例
你可以嘗試執行以下程式碼來了解如何使用 insertBefore() 方法
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("<span>Demo text</span>").insertBefore("p");
});
});
</script>
</head>
<body>
<button>Insert</button>
<br>
<p>This is a paragraph.</p>
</body>
</html>
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP