var classVal = document.getElementById("id").getAttribute("class");
//删除的话 classVal = classVal.replace("someClassName",""); document.getElementById("id").setAttribute("class",classVal ); //添加的话 classVal = classVal.concat(" someClassName"); document.getElementById("id").setAttribute("class",classVal ); //替换的话 classVal = classVal.replace("someClassName","otherClassName"); document.getElementById("id").setAttribute("class",classVal );
转载于:https://www.cnblogs.com/sunzhnan/p/8288078.html