青岛网站建设新闻资讯

用JS添加和删除class类名

时间:2023-01-19 已阅读:991次 | 作者:青岛网站建设

首页>新闻资讯>建站知识

今天给大家分享的建站知识是:用JS添加和删除class类名。

方法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.qinghuahulian.com/news/webzhishi/1369.html

相关资讯

Copyright © 2011-2024 青华互联-青岛青华锐思网络科技有限公司 www.qinghuahulian.com All Rights Reserved
鲁公网安备37020202000800号 鲁ICP备14020555号-4 网站地图 百度地图