如何在 jQuery 中使用 attr() 方法來獲取屬性的 value?


jQuery attr() 方法用於獲取屬性的值(例如href)。將屬性作為 attr() 方法的引數提及。

你可以嘗試執行以下程式碼,瞭解如何使用 attr() 方法獲取屬性的值

示例

動態演示

<html>
   <head>
      <title>Selector 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($("#tpoint").attr("href"));
             });
         });
      </script>
   </head>
   <body>
      <p><a href="https://tutorialspoint.tw" id="tpoint"> Website tutorialspoint</a></p>
      <button>Show complete link</button>
   </body>
</html>

更新於: 13-Feb-2020

362 次瀏覽

開啟你的職業生涯

完成課程,取得認證

開始吧
廣告
© . All rights reserved.