HasClass,用于 if 语句中的多个值

我有一个简单的 if 语句:

if ($('html').hasClass('m320')) {


// do stuff


}

一切如我所料。但是,我想向 if statement添加更多的类,以检查在 <html>标记中是否存在任何类。我需要它,所以它不是所有的,但只是存在至少一个类,但它可以更多。

我的用例是,我为不同的视口宽度添加了类(例如 m320m768) ,所以我只想执行特定的 Jquery,如果它是一个特定的宽度(类)。

以下是我到目前为止所做的尝试:

1.

if ($('html').hasClass('m320', 'm768')) {


// do stuff


}

2.

if ($('html').hasClass('m320')) || ($('html').hasClass('m768')) {


// do stuff


}

3.

 if ($('html').hasClass(['m320', 'm768'])) {


// do stuff


}

不过这些似乎都不管用。不确定我做错了什么,但很可能是我的语法或结构。

113203 次浏览

You just had some messed up parentheses in your 2nd attempt.

var $html = $("html");


if ($html.hasClass('m320') || $html.hasClass('m768')) {


// do stuff


}

You could use is() instead of hasClass():

if ($('html').is('.m320, .m768')) { ... }
var classes = $('html')[0].className;


if (classes.indexOf('m320') != -1 || classes.indexOf('m768') != -1) {
//do something
}

For fun, I wrote a little jQuery add-on method that will check for any one of multiple class names:

$.fn.hasAnyClass = function() {
for (var i = 0; i < arguments.length; i++) {
if (this.hasClass(arguments[i])) {
return true;
}
}
return false;
}

Then, in your example, you could use this:

if ($('html').hasAnyClass('m320', 'm768')) {


// do stuff


}

You can pass as many class names as you want.


Here's an enhanced version that also lets you pass multiple class names separated by a space:

$.fn.hasAnyClass = function() {
for (var i = 0; i < arguments.length; i++) {
var classes = arguments[i].split(" ");
for (var j = 0; j < classes.length; j++) {
if (this.hasClass(classes[j])) {
return true;
}
}
}
return false;
}


if ($('html').hasAnyClass('m320 m768')) {
// do stuff
}

Working demo: http://jsfiddle.net/jfriend00/uvtSA/

Here is a slight variation on answer offered by jfriend00:

$.fn.hasAnyClass = function() {
var classes = arguments[0].split(" ");
for (var i = 0; i < classes.length; i++) {
if (this.hasClass(classes[i])) {
return true;
}
}
return false;
}

Allows use of same syntax as .addClass() and .removeClass(). e.g., .hasAnyClass('m320 m768') Needs bulletproofing, of course, as it assumes at least one argument.

This may be another solution:

if ($('html').attr('class').match(/m320|m768/)) {
// do stuff
}

according to jsperf.com it's quite fast, too.

For anyone wondering about some of the different performance aspects with all of these different options, I've created a jsperf case here: jsperf

In short, using element.hasClass('class') is the fastest.

Next best bet is using elem.hasClass('classA') || elem.hasClass('classB'). A note on this one: order matters! If the class 'classA' is more likely to be found, list it first! OR condition statements return as soon as one of them is met.

The worst performance by far was using element.is('.class').

Also listed in the jsperf is CyberMonk's function, and Kolja's solution.

The hasClass method will accept an array of class names as an argument, you can do something like this:

$(document).ready(function() {
function filterFilesList() {
var rows = $('.file-row');
var checked = $("#filterControls :checkbox:checked");


if (checked.length) {
var criteriaCollection = [];


checked.each(function() {
criteriaCollection.push($(this).val());
});


rows.each(function() {
var row = $(this);
var rowMatch = row.hasClass(criteriaCollection);


if (rowMatch) {
row.show();
} else {
row.hide(200);
}
});
} else {
rows.each(function() {
$(this).show();
});
}
}


$("#filterControls :checkbox").click(filterFilesList);
filterFilesList();
});

Try this:

if ($('html').hasClass('class1 class2')) {


// do stuff


}

This is in case you need both classes present. For either or logic just use ||

$('el').hasClass('first-class') || $('el').hasClass('second-class')

Feel free to optimize as needed