如何使用 jQuery 更改屬性值?


jQuery attr() 方法用於獲取屬性值。它還可用於更改值。在這個示例中,我們將更改屬性值 tutorialspoint.comtutorialspoint.com/java

您可以嘗試執行以下程式碼來學習如何使用 jQuery 更改屬性值 -

示例

即時演示

<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(){
           $("#tpoint").attr("href", "https://tutorialspoint.tw/java");
         });
       });
      </script>
   </head>
   <body>
      <p><a href="https://tutorialspoint.tw" id="tpoint">tutorialspoint.com</a></p>
      <button>Change attribute value</button>
      <p>The value of above link will change on clicking the above button. Check before and after clicking the link.</p>
   </body>
</html>

更新日期:2020-02-13

8K+ 瀏覽量

開啟你的 職業生涯

完成該課程後可獲得認證

開始課程
廣告
© . All rights reserved.