测试HTML属性tabindex是否存在并获取值
要获取HTML属性的值,请尝试以下操作:
$("#demo").attr("tabindex")该attr()方法可用于从匹配集中的第一个元素中获取属性的值,或将属性值设置到所有匹配元素上。
您也可以使用该hasAttribute()方法查看元素是否有属性。
示例
尝试运行以下代码以了解如何使用hasAttribute()方法:
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.staticfile.org/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>