Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/460.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检查css类是否存在,如果存在,检查次数_Javascript_Jquery_Html_Css_Dom - Fatal编程技术网

Javascript 检查css类是否存在,如果存在,检查次数

Javascript 检查css类是否存在,如果存在,检查次数,javascript,jquery,html,css,dom,Javascript,Jquery,Html,Css,Dom,我想知道样式类是否存在,如果存在,有多少元素 要知道样式类是否存在,我使用: if ($("*").hasClass('ui-state-active')) { alert("class exist : "+nb_checked); } 但是要知道这个类有多少个元素,我无法计算。这是一个更简单的方法: $('.ui-state-active').length 只要做: $('.ui-state-active').length 这两种方法都可以使用 c = $('.ui-state-

我想知道样式
类是否存在,如果存在,有多少元素

要知道样式
类是否存在,我使用:

if ($("*").hasClass('ui-state-active')) {
    alert("class exist : "+nb_checked);
}

但是要知道这个类有多少个元素,我无法计算。

这是一个更简单的方法:

$('.ui-state-active').length
只要做:

$('.ui-state-active').length

这两种方法都可以使用

c = $('.ui-state-active').length;
if (c>0) {
    console.log('There is '+c+' elements having required class');
}

您可以一次性完成这两项工作:

var elements = $('.ui-state-active');
if(elements.length === 0) {
    alert('No elements with class ui-state-active!')
} else {
    alert(elements.length +  ' elements with class ui-state-active');
}
那么:

$(".ui-state-active").length;
文件在此:


使用jQuery,您可以直接选择具有特定类的。此选项的语法与CSS选择器的语法相同:

$(".className")
这将创建一个,它是匹配元素的集合。此对象有许多有用的属性,其中之一是集合中的元素数

在您的情况下,查找所需元素的数量就像查找

$(".ui-state-active").length
$(".ui-state-active").length