jQuery hasClass()的应用实例
hasClass()是jQuery内置的一个方法,用于检查指定的类名的元素是否存在。
语法:
$(selector).hasClass(className);
参数:它接受一个 “className “参数,指定需要在所选元素中搜索的类名称。
返回值:如果搜索成功,它将返回true,否则返回false。
jQuery代码显示此方法的工作:
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
(document).ready(function() {
("button").click(function() {
alert($("p").hasClass("find"));
});
});
</script>
<style>
.find {
font-size: 120%;
color: green;
}
body {
width: 50%;
height: 200px;
border: 2px solid green;
padding: 20px;
}
</style>
</head>
<body>
<h1>Heading 1</h1>
<p class="find">Geeks for Geeks !.</p>
<p>This is normal paragraph.</p>
<button>Click me!</button>
</body>
</html>
输出:
在点击 “点击我!”按钮之前-
在点击 “点击我!”按钮后–