例如,我有以下CSS:
#main {
width: 150px;
}
#main.active {
width: 250px;
}
如果我要使用JS添加或删除类active
;在IE8中,元素的宽度保持为150px。我尝试添加!important
,但它甚至没有发现className已被更改。这有什么解决方法吗?这些是使用的功能:
tabScroller.onclick = function() {
if(hasClass(this.parentNode, 'active')) removeClass(this.parentNode, 'active');
else addClass(this.parentNode, 'active');
}
if(typeof String.prototype.trim !== 'function') {
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g, '');
}
}
function hasClass(elem, theClass) {
var className = elem.className;
if(className.indexOf(theClass) < 0) return false;
else return true;
}
function addClass(elem, theClass) {
var className = elem.className;
className += ' ' + theClass;
elem.className = className.trim();
return false;
}
function removeClass(elem, theClass) {
var className = elem.className;
className = className.replace(theClass, '');
elem.className = className.trim();
return false;
}