如何使用 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>
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP