javascript - How to implement jq's addclass and removeclass in js. For example, it is originally class="aa" and needs to be changed to class="aa bb"
怪我咯2017-07-05 10:44:24
0
5
1105
How to implement jq's addclass and removeclass in js. For example, it is originally class="aa" and it needs to be changed to class="aa bb". Be careful not to use jquery
elem.className or elem.classList I feel classList is better, elem.classList.add('bb') will do In addition, the jQuery source code implements addClass and removeClass through className Finally, this bias For basic questions, you need to learn how to Baidu and Google yourself
js has a classlist interface
MDN link
But you have to consider compatibility.
replace
if (!this.hasClass(obj, class)) obj.className += " " + class;
elem.className
or
elem.classList
I feel classList is better, elem.classList.add('bb') will do
In addition, the jQuery source code implements addClass and removeClass through className
Finally, this bias For basic questions, you need to learn how to Baidu and Google yourself