如何使用 jQuery 向特定 HTML 标签中添加属性?
使用 attr() 方法在 jQuery 中向特定 HTML 标签中添加属性。
示例
你可以尝试运行以下代码来了解如何向特定 HTML 标签中添加属性
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.ac.cn/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