document.getElementById("MyElement").className = " MyClass";' J3 X9 A+ X' S8 T0 U. n
删除元素中的一个类:在不影响其他潜在类别的情况下,需要简单的正则表达替换:7 X, |; O. A# C3 T0 r" g$ a
document.getElementById("MyElement").className = document.getElementById("MyElement").className.replace ^|\s)MyClass(?!\S)/g ,'' )/* Code wrapped for readability - above is all one statement */( m7 p9 d# B( x" Y: D1 J
正则表达式的解释如下: : u7 P; y# R! E4 Z& b, \
(?:^|\s) # Match the start of the string or any single whitespace characterMyClass # The literal text for the classname to remove(?!\S) # Negative lookahead to verify the above is the whole classname # Ensures there is no non-space character following # (i.e. must be the end of the string or space) / X) D5 n* H+ `6 V