如何使用 jQuery 更改属性值?

jqueryweb developmentfront end technology

jQuery attr() 方法用于获取属性值。它也可以用于更改属性值。在本例中,我们将属性值 tutorialspoint.com 更改为 tutorialspoint.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://www.tutorialspoint.com/java");
         });
       });
      </script>
   </head>
   <body>
      <p><a href="https://www.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>

相关文章