table of Contents
jQuery.hasClass () function Detailed
hasClass()
Function is used to indicate the current element jQuery whether the matched objects containing specified css class name .
This function belongs to jQuery
the object (instance).
grammar
JavaScript:
jQueryObject.hasClass( className )
parameter
parameter | description |
---|---|
className | String type specified css class name. |
return value
hasClass()
The return value is a Boolean type, returns a boolean value specifies whether to include css class names, if you include returns true
, otherwise it returns false
.
If the current jQuery object matching a plurality of elements, as long as there is any element comprising css specified class name is returned true
.
Examples & Description
hasClass(className)
Function is equivalent to is(".className")
:
JavaScript:
$element.hasClass( className );
// 等价于
$element.is( "." + className );
HTML paragraph with the following code as an example:
HTML:
<div id="n1">
<p id="n2" class="site-name">CodePlayer</p>
<p id="n3" class="foo bar demo">专注于编程开发技术分享</p>
</div>
We write the following jQuery code to demonstrate the hasClass()
use of the function:
JavaScript:
var $n2 = $("#n2");
document.writeln( $n2.hasClass("site-name") ); // true
var $n3 = $("#n3");
document.writeln( $n3.hasClass("bar") ); // true
// 不存在该css类名,返回false
document.writeln( $n3.hasClass("noClass") ); // false
var $p = $("p");
// 只要jQuery对象匹配的元素中有任意一个元素包含指定的css类名,即返回true
document.writeln( $p.hasClass("site-name") ); // true
document.writeln( $p.hasClass("foo") ); // true