如何使用 jQuery hasAttribute()方法檢視元素中是否有屬性?


使用 jQuery hasAttribute() 方法檢視元素是否有屬性。

示例

你可以嘗試執行以下程式碼學習如何使用 hasAttribute() 方法

即時演示

<!DOCTYPE html>
<html>
   <head>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
      <script>
         $(document).ready(function(){
           $('button').on('click', function() {

             if (this.hasAttribute("style")) {
               alert('True')
             } else {
               alert('False')
             }
             
          })
         });
      </script>
   </head>
   <body>
       Check for attribute in the button element<br>
      <button class='button' style='background-color:blue;'>
      Button
      </button>
      <button class='button'>
      Button 2
      </button>
   </body>
</html>

更新於: 2019 年 12 月 18 日

2 千瀏覽量

開啟你的 職業

完成課程獲取認證

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