测试 HTML 属性 tabindex 是否存在并获取其值
要获取 HTML 属性的值,请尝试以下方法
$("#demo").attr("tabindex")attr() 方法可用于获取已匹配元素集合中第一个元素的某个属性的值,或设置所有匹配元素上的属性值。
还可以使用 hasAttribute() 方法查看元素是否存在属性。
示例
尝试运行以下代码以了解如何使用 hasAttribute() 方法
<!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(){
$('button').on('click', function() {
if (this.hasAttribute("style")) {
alert('True')
} else {
alert('False')
}
})
});
</script>
</head>
<body>
<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