Javascript 比较项目类别以执行函数(变量?)

Javascript 比较项目类别以执行函数(变量?),javascript,jquery,class,comparison,Javascript,Jquery,Class,Comparison,在Jquery中,我想比较h3和div的类。但是,我有很多这样的类,我不能进行大小写切换,因为将来可能会添加更多这样的类。以下是一个例子: <h3 class="one">One</h3> <h3 class="two">Two</h3> <h3 class="three">Three</h3> <div class="one">One</div> <div class="two">Two

在Jquery中,我想比较h3和div的类。但是,我有很多这样的类,我不能进行大小写切换,因为将来可能会添加更多这样的类。以下是一个例子:

<h3 class="one">One</h3>
<h3 class="two">Two</h3>
<h3 class="three">Three</h3>
<div class="one">One</div>
<div class="two">Two</div>
<div class="three">Three</div>

不必陈述每一个班级比较的案例?可能是一个变量或什么?

给定的
h3
div
是DOM节点,每个节点只有一个类

if (h3.className === div.className) {
    /* runs if both h3 and div have the same class attribute value.
}
对于多个类,我建议您使用ES5和classlist

if (Array.prototype.every.call(h3.classList, function(v) {
        return div.classList.contains(v);
    })
) {
    /* runs if both h3 and div share the same classes */
}

在第一个示例中,我还需要定义类吗?重点是我将有50个这样的东西,我想让它检测这些类是否相同,而不需要实际说明每个类。@user7885不,你不需要。它通过比较
className
classList
if (Array.prototype.every.call(h3.classList, function(v) {
        return div.classList.contains(v);
    })
) {
    /* runs if both h3 and div share the same classes */
}