如何在 jQuery 中获取 id 属性的值?

jqueryweb developmentfront end technology

使用 jQuery 的 attr() 方法获取 id 属性的值。您可以尝试运行以下代码来学习如何在 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() {
            alert($('#div1').attr('id'));
         });
      </script>
       
   </head>
   
   <body>
      <div id = "div1">
         <p>This is demo text.</p>
      </div>
   </body>
</html>

相关文章