Clear element.classList

前端 未结 7 1497
暗喜
暗喜 2021-02-04 23:38

element.classList is of DOMTokenList type.

Is there a method to clear this list?

7条回答
  •  温柔的废话
    2021-02-05 00:02

    With ES6 and the spread operator, this is a breeze.

    element.classList.remove(...element.classList);
    

    This will spread the list items as arguments to the remove method.
    Since the classList.remove method can take many arguments, they all are removed and the classList is cleared.

    Even though it is readable it is not very efficient. @Fredrik Macrobond's answer is faster.
    View different solutions and their test results at jsperf.

提交回复
热议问题