如何用 jQuery 在 div 中找到錨標籤並新增類?


要找到 div 中的錨標籤,請使用選擇器,addClass() 方法使用 jQuery 新增類。

你可以嘗試執行以下程式碼,瞭解如何在 div 中找到錨標籤並新增類

線上演示

<html>
   <head>
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
      <script>
         $(document).ready(function(){
           $("#button1").click(function(){
             $('#div1 a').addClass('myclass');
           });
        });
      </script>
     
         <style>
          .myclass {
             font-size: 25px;
           }
         </style>

   </head>
   <body>
      <div id="div1">
         <a href="http://www.google.com">Demo</a>
      </div>
      <div id="div2">
         <p>This is demo text.</p>
      </div>
      <button id="button1">Click</button>
   </body>
</html>

更新於: 2020 年 6 月 12 日

3000 次瀏覽

開啟你的 職業生涯

完成課程以獲得認證

入門
廣告
© . All rights reserved.