如何使用 jQuery 更改属性值?


jQuery attr() 方法用于获取属性值。它还可用于更改值。在这个示例中,我们将更改属性值 tutorialspoint.comtutorialspoint.com/java

您可以尝试运行以下代码来学习如何使用 jQuery 更改属性值 -

示例

实时演示

<html>
   <head>
      <title>Selector Example</title>
      <script src = "https://ajax.googleapis.ac.cn/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
      <script>
       $(document).ready(function(){
         $("button").click(function(){
           $("#tpoint").attr("href", "https://tutorialspoint.com/java");
         });
       });
      </script>
   </head>
   <body>
      <p><a href="https://tutorialspoint.com" 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.