发现任何元素在 jQuery 中是否有任何类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14631547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Discovering if any element has any class in jQuery
提问by Kev
I have inherited some jQuery and HTML and need to find out if any of the HTML elements have a class name of any value. I've read around a few threads and Googled but can't find anything useful. So in pseudo-code what I'd like is:
我继承了一些 jQuery 和 HTML,需要找出是否有任何 HTML 元素具有任何值的类名。我已经阅读了一些主题并在谷歌上搜索,但找不到任何有用的东西。所以在伪代码中我想要的是:
Loop through all HTML elements
If any of them have any class at all:
Get their class name as a string
Hoping that makes sense!
希望这是有道理的!
回答by Hymantheripper
Try:
尝试:
$('*').each(function() {
if ($(this).hasClass()) {
var class_name = $(this).attr('class');
// do something
}
});
Why you would want to do this, I have no idea. It is veryinefficient
你为什么要这样做,我不知道。这是非常低效的
回答by fwiw
For what it's worth:
物有所值:
$(this).hasClass()
always returns false.
总是返回false。
$("[class]")
doesn't work either, as it returns elements like
也不起作用,因为它返回类似的元素
<p class=""> my p </p>
jsfiddle for both here: http://jsfiddle.net/JZ8LV/1/
jsfiddle 在这里:http: //jsfiddle.net/JZ8LV/1/
Solution:
解决方案:
$(this).hasClass('')
returns truefor elements withouta class, including those of the form
对于没有类的元素返回true,包括表单中的元素
<p class=""> my p </p>
so
所以
$('*').each(function() {
if ($(this).hasClass('') === false) {
$("body").append($(this).prop("tagName") + " has a proper class defined <br/>");
}
});
returns all the tags with a proper class defined.
返回具有适当类定义的所有标签。
Jsfiddle: http://jsfiddle.net/2Rtj5/
jsfiddle:http: //jsfiddle.net/2Rtj5/
回答by Viktor S.
You can use something like this:
你可以使用这样的东西:
$("[class]").each(function(){
var className = $(this).attr("class");
})
Demo: http://jsfiddle.net/L5WAV/1/(see results in console - two divs should be found)
演示:http: //jsfiddle.net/L5WAV/1/(在控制台查看结果 - 应该找到两个 div)
回答by Konrad Gadzina
To get all elements with class you can use $('*[class]')
:
要使用类获取所有元素,您可以使用$('*[class]')
:
$('*[class]').each(function(){
// do what you want
});
You can even easily count them with $('*[class]').length
.
您甚至可以使用 轻松计算它们$('*[class]').length
。
回答by GNi33
var classObj = {};
$('*').each(function(){
var cNames = this.className.split(' ');
for(var i=0, l=cNames.length; i<l; i+=1){
classObj[cNames[i]] = true;
}
});
That would give you an Object (classObj
) with every appearing class of the document as property, so a class name won't show up multiple times if there are elements with the same class in the document. I really would not do this or see any use-case in this though.
这将为您提供一个 Object ( classObj
),其中每个出现的文档类都作为属性,因此如果文档中存在具有相同类的元素,则类名不会多次出现。尽管如此,我真的不会这样做或看到任何用例。
回答by gsamaras
Not a direct question, but I needed it, so here it goes for future users.
不是一个直接的问题,但我需要它,所以它适用于未来的用户。
In case you want to find if anyelement has a specificclass, you could use this function:
如果要查找任何元素是否具有特定类,可以使用此函数:
function find_any_element(className) {
var found = false;
$('*').each(function() {
if ($(this).hasClass(className)) {
var class_name = $(this).attr('class');
alert("There is an element with this class: " + class_name);
found = true;
}
});
if(!found) {
alert("No element with className '" + className + "' found.");
}
}
回答by johannes
No need to loop over your elements:
无需遍历您的元素:
if( jQuery( ".myClass" ).is( ":visible" ) )
{
// do your thing
}
The downside is that your element cannot have the display
-property set to none
缺点是您的元素不能将display
-property 设置为none