jQuery before() 方法
jQuery 中的 before() 方法用於在選定的元素之前插入指定內容。
示例
現在讓我們看一個示例來實現 jQuery 中的 before() 方法 −
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#btndemo").click(function(){
$("img").before("<strong>Text inserted before!</strong>");
});
});
</script>
</head>
<body>
<img src="https://tutorialspoint.tw/images/tp-logo-diamond.png" alt="Tutorialspoint Logo" width="220" height="120"><br><br>
<button id="btndemo">Insert specified content</button>
</body>
</html>輸出
這將產生以下輸出 −

點選“插入指定內容” −

廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式語言
C++
C#
MongoDB
MySQL
Javascript
PHP