要查找jQuery中所选项目是否存在某个属性,请使用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("myattr")) {
alert('True - myattr attribute exists')
} else {
alert('False - myattr attribute does not exist')
}
})
});
</script>
</head>
<body>
<button class='button' style='color: green' myattr='demo'>
Button One
</button>
<button class='button'>
Button Two
</button>
</body>
</html>