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>

輸出

這將產生以下輸出 −

點選“插入指定內容” −

更新於:2019 年 11 月 11 日

143 次瀏覽

開啟 職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.