由於一個 div 裡有多個 class 想要增加/移除, 直接存取屬性值可能會蓋掉一些別人寫入的值. 所以
For adding class:
document.getElementById("div1").classList.add("classToBeAdded");
For removing class:
document.getElementById("div1").classList.remove("classToBeRemoved");
If you’re only targeting modern browsers:
Use element.classList.add to add a class:
element.classList.add("my-class");
And element.classList.remove to remove a class:
element.classList.remove("my-class");
資料來源
How to add and remove classes in Javascript without jQuery
https://stackoverflow.com/questions/26736587/how-to-add-and-remove-classes-in-javascript-without-jquery
How to add a class to a given element?
https://stackoverflow.com/questions/507138/how-to-add-a-class-to-a-given-element