如何在 jQuery 中獲取 id 屬性的值?


使用 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>

更新日期: 2020 年 2 月 13 日

2,000+ 次瀏覽

開啟您的 職業生涯

完成課程以獲得認證

立即開始
廣告
© . All rights reserved.