Home  >  Article  >  Web Front-end  >  How to use jQuery hasClass()

How to use jQuery hasClass()

藏色散人
藏色散人Original
2020-11-10 13:54:122408browse

The jQuery hasClass() method is used to check whether the selected element contains the specified class name. Its usage syntax is "$(selector).hasClass(classname)". The parameter classname indicates that it needs to be searched in the selected element. the type.

How to use jQuery hasClass()

Recommended: "jquery video tutorial"

jQuery hasClass() definition and usage

hasClass() method checks whether the selected element contains the specified class name.

If the selected element contains the specified class, this method returns "true".

Syntax

$(selector).hasClass(classname)

Parameters

classname required. Specifies the class to be searched for in the selected elements.

Example

Check whether the

element contains "intro" class:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>PHP中文网</title>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
	$("button").click(function(){
		alert($("p").hasClass("intro"));
	});
});
</script>
<style type="text/css">
.intro{
	font-size:120%;
	color:red;
}
</style>
</head>
<body>

<h1>这是一个段落标题</h1>
<p class="intro">这是一个段落</p>
<p> 这是另外一个段落</p>
<button>是否有 p 元素使用了 "intro" 类?</button>

</body>
</html>

Running effect:

How to use jQuery hasClass()

The above is the detailed content of How to use jQuery hasClass(). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn