要使用jQuery检查元素是否具有特定的类名,请使用hasClass()方法。在下面的示例中,您可以尝试运行以下代码来检查元素是否具有特定的类名,即“ myclass”:
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
alert($("h2").hasClass("myclass"));
});
});
</script>
<style>
.myclass {
color: blue;
}
</style>
</head>
<body>
<h2 class="myclass">Heading</h1>
<p>This is demo text.</p>
<button>Check: The h2 element has 'myclass' class?</button>
</body>
</html>