jQuery 中的 jQuery.andSelf() 方法的功能是什麼?


andSelf() 方法將之前的選擇項新增到當前選擇項中。當你在指令碼中有多次遍歷並隨後新增在上次遍歷之前匹配的某項內容時,該方法非常有用。

示例

你可以嘗試執行以下程式碼瞭解如何使用 jQuery.andSelf() 方法

即時演示

<html>
   <head>
      <title>jQuery andSelf() method</title>
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>    
      <script>
         $(document).ready(function(){
            $("div").find("p").andSelf().addClass("border");
         });
      </script>    
      <style>
         p, div {
            margin:5px;
            padding:5px;
         }
         .border {
            border: 2px solid red;
         }
         .background {
            background:yellow;
         }
      </style>    
   </head>
   <body>
      <div>
         <p>First Paragraph</p>
         <p>Second Paragraph</p>
      </div>    
   </body>
</html>

更新時間: 2019 年 12 月 9 日

99 次瀏覽

開啟你的職業生涯

完成課程並獲得認證

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