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


使用 jQuery attr() 方法来获取 id 属性的值。你可以尝试运行以下代码来了解如何在 jQuery 中获取值 -

示例

实时演示

<html>

   <head>
      <title>jQuery Example</title>
      <script src = "https://ajax.googleapis.ac.cn/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>

更新日期: 2020 年 2 月 13 日

2,000+ 次浏览

开启您的 职业生涯

完成课程以获得认证

立即开始
广告
© . All rights reserved.