到目前为止,我必须这样做:

elem.classList.add("first");
elem.classList.add("second");
elem.classList.add("third");

这在jQuery中是可以实现的,就像这样

$(elem).addClass("first second third");

我想知道是否有任何本地的方法来添加或删除。


当前回答

elem.classList.add("first");
elem.classList.add("second");
elem.classList.add("third");

是相等的

elem.classList.add("first","second","third");

其他回答

elem.classList.add("first");
elem.classList.add("second");
elem.classList.add("third");

是相等的

elem.classList.add("first","second","third");

我喜欢@rich。kelly的答案,但我想使用与classList.add()(逗号分隔字符串)相同的命名法,因此稍有偏差。

DOMTokenList.prototype.addMany = DOMTokenList.prototype.addMany || function() {
  for (var i = 0; i < arguments.length; i++) {
    this.add(arguments[i]);
  }
}
DOMTokenList.prototype.removeMany = DOMTokenList.prototype.removeMany || function() {
  for (var i = 0; i < arguments.length; i++) {
    this.remove(arguments[i]);
  }
}

所以你可以使用:

document.body.classList.addMany('class-one','class-two','class-three');

我需要测试所有的浏览器,但这适用于Chrome。 我们是否应该检查一些比DOMTokenList.prototype.addMany的存在更具体的东西?究竟是什么原因导致classList.add()在IE11中失败?

最好的解决方案之一是检查元素是否存在,然后继续添加或删除元素,最重要的是,如果元素为空,则删除它。

/** * @description detect if obj is an element * @param {*} obj * @returns {Boolean} * @example * see below */ function isElement(obj) { if (typeof obj !== 'object') { return false } let prototypeStr, prototype do { prototype = Object.getPrototypeOf(obj) // to work in iframe prototypeStr = Object.prototype.toString.call(prototype) // '[object Document]' is used to detect document if ( prototypeStr === '[object Element]' || prototypeStr === '[object Document]' ) { return true } obj = prototype // null is the terminal of object } while (prototype !== null) return false } /* * Add multiple class * addClasses(element,['class1','class2','class3']) * el: element | document.querySelector(".mydiv"); * classes: passing:: array or string : [] | 'cl1,cl2' | 'cl1 cl2' | 'cl1|cl2' */ function addClasses(el, classes) { classes = Array.prototype.slice.call(arguments, 1); if ( isElement(el) ){ //if (document.body.contains(el) for (var i = classes.length; i--;) { classes[i] = Array.isArray(classes[i]) ? classes[i]: classes[i].trim().split(/\s*,\s*|\s+/); for (var j = classes[i].length; j--;) el.classList.add(classes[i][j]); } } } /* * Remove multiple class * Remove attribute class is empty * addClasses(element,['class1','class2','class3']) * el: element | document.querySelector(".mydiv"); * classes: passing:: array or string : [] | 'cl1,cl2' | 'cl1 cl2' | 'cl1|cl2' */ function removeClasses(el, classes) { classes = Array.prototype.slice.call(arguments, 1); if ( isElement(el) ) { for (var i = classes.length; i--;) { classes[i] = Array.isArray(classes[i]) ? classes[i]: classes[i].trim().split(/\s*,\s*|\s+/); for (var j = classes[i].length; j--;) el.classList.remove(classes[i][j]); let cl = el.className.trim(); if (!cl){ el.removeAttribute('class'); } } } } var div = document.createElement("div"); div.id = 'test'; // div.setAttribute("id", "test"); div.textContent = 'The World'; //div.className = 'class'; // possible use: afterend, beforeend document.body.insertAdjacentElement('beforeend', div); // Everything written above you can do so: //document.body.insertAdjacentHTML('beforeend', '<div id="text"></div>'); var el = document.querySelector("#test"); addClasses(el,'one,two,three,four'); removeClasses(el,'two,two,never,other'); el.innerHTML = 'I found: '+el.className; // return : I found: four three one #test { display: inline-block; border: 1px solid silver; padding: 10px; }

新的展开操作符使应用多个CSS类作为数组更加容易:

const list = ['first', 'second', 'third'];
element.classList.add(...list);

假设你有一个要添加的类数组,你可以使用ES6扩展语法:

Let classes = ['first', 'second', 'third']; elem.classList.add(…类);