如何使用 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 千+瀏覽

啟動你的 事業

完成課程後進行認證

開始
廣告