如何在 jQuery 中獲取屬性值?


在 jQuery 中獲取屬性值相當容易。為此,請使用 jQuery attr() 方法。您可以嘗試執行以下程式碼來了解如何在 jQuery 中獲取屬性值 −

示例

線上演示

<html>

   <head>
      <title>jQuery Example</title>
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

      <script>
      $(document).ready(function(){
         $("button").click(function(){
            alert($("img").attr("height"));
         });
      });
      </script>
   </head>
   
   <body>
      <img src="/green/images/logo.png" alt="logo" width="350" height="120"><br>
      <button>Get the height</button>
   </body>
   
</html>

更新於: 13-02-2020

2K+ 瀏覽

開啟您的 職業生涯

完成課程獲取認證

開始
廣告
© . All rights reserved.