使用纯JavaScript(不是jQuery),有没有办法检查元素包含是否为类?
目前,我正在这样做:
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...
<div id="test" class="class1 class5"></div>
…不再有完全匹配,所以我得到的默认输出为无(""
)。但我仍然希望输出为I have class1
,因为<div>
仍然是包含.class1
类。