原生js添加和删除类名

方法1.

添加:document.getElementById("id").classList.add("类名");

删除:document.getElementById("id").classList.remove("类名");

方法2

var classVal=document.getElementById("id").getAttribute("class");

添加:document.getElementById("id").setAttribute("class",classVal.concat(" 类名"));

删除:document.getElementById("id").getAttribute("class").replace("类名"," ");

方法3.

添加:document.getElementById("id").className+=" 类名";

来源:https://www.cnblogs.com/zml-mary/p/8574424.html

posted @ 2019-02-20 10:38  chengzi_he  阅读(553)  评论(0编辑  收藏  举报