如何在 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 日

2K+ 次瀏覽

開啟您的 職業生涯

完成課程後獲得認證

開始
廣告
© . All rights reserved.