Using plain JavaScript (not jQuery), Is there any way to check if an element contains a class?(使用普通的JavaScript(不是jQuery),是否可以检查元素是否包含类?)
Currently, I'm doing this:(目前,我正在这样做:)
var test = document.getElementById("test"); var testClass = test.className; switch (testClass) { case "class1": test.innerHTML = "I have class1"; break; case "class2": test.innerHTML = "I have class2"; break; case "class3": test.innerHTML = "I have class3"; break; case "class4": test.innerHTML = "I have class4"; break; default: test.innerHTML = ""; }
<div id="test" class="class1"></div>
The issue is that if I change the HTML to this...(问题是,如果我将HTML更改为此...)
<div id="test" class="class1 class5"></div>
...there's no longer an exact match, so I get the default output of nothing ( ""
).(...不再有完全匹配的内容,因此我得到的默认输出为none( ""
)。) But I still want the output to be I have class1
because the <div>
still contains the .class1
class.(但是我仍然希望输出为I have class1
因为<div>
仍然包含 .class1
类。)
ask by daGUY translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…