您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要为大家展示了“原生js如何添加一个或多个类名”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“原生js如何添加一个或多个类名”这篇文章吧。
具体如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css" media="screen">
#box {
width: 200px;
height: 200px;
margin: 0 auto;
background: pink;
border-radius: 20px;
}
@keyframes move{
0%{width: 500px; background-color: red;}
10%{width: 100px; background-color: green;},
20%{width: 900px; background-color: yellow;},
50%{width: 300px; background-color: pink;},
80%{width: 550px; background-color: gold;},
100%{width: 200px; background-color: purple;}
}
.movea {
animation: move 1s ;
}
</style>
</head>
<body>
<div id='box'>
</div>
<script type="text/javascript">
document.getElementById('box').onclick = function() {
this.setAttribute("class", "movea");
this.classList.add("movea");
}
</script>
</body>
</html>二 .使用原生JS给元素--添加/删除类名
var dom = document.getElementById("idName");添加 单个 class:
dom.classList.add("className1");添加多个类:
dom.classList.add("className1", "className2", "className3", ....., "classNameN");移除一个类:
dom.classList.remove("className1");移除多个类:
dom.classList.remove("className1", "className2", "className3", ....., "classNameN");检查是否含有某个类
dom.classList.contains('className'); //return true or false当然还有很多其他的方法,上面的也不全.
以上是“原生js如何添加一个或多个类名”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。