如何使用 jQuery 向特定 HTML 標籤中新增屬性?
使用 attr() 方法在 jQuery 中向特定 HTML 標籤中新增屬性。
示例
你可以嘗試執行以下程式碼來了解如何向特定 HTML 標籤中新增屬性
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(document).ready(function(){
$("button.button1").attr("myid", "myvalue");
$('button').on('click', function() {
if (this.hasAttribute("myid")) {
alert('True: The new attribute added successfully.')
} else {
alert('False')
}
})
});
});
</script>
</head>
<body>
<button class='button1'>
Button 1
</button>
</body>
</html>
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP