想知道是否有与 some_element.classList.add("some_class", "some_other_class).remove("some_other_other_class")
行等效的功能。
我知道 .toggle()
方法在某些情况下是一种解决方案,但我想知道是否有一种方法可以精确使用 .add()
和.remove()
在一行上。
最佳答案
是的,您可以使用classList.replace()方法
element.classList.replace('some-class', 'new-class');
关于javascript - JS 是否可以在一行中向 html 元素添加一些类并删除一些其他类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62865511/