码迷,mamicode.com
首页 > Web开发 > 详细

纯js添加类

时间:2017-11-23 08:34:42      阅读:267      评论:0      收藏:0      [点我收藏+]

标签:class   .class   htm   text   doctype   type   att   set   back   

1、el.setAttribute(‘class‘,‘abc‘); 

<!DOCTYPE HTML>
<HTML>
<HEAD>
<meta charset="utf-8" />
<title>setAttribute(‘class‘, ‘abc‘)</title>
<style type="text/css">
.abc {
background: red;
}
</style>
</HEAD>
<BODY>
<div id="d1">test div</div>
<script>
var div = document.getElementById(‘d1‘);
div.setAttribute("class", "abc");
</script>
</BODY>
</HTML>
2、el.setAttribute(‘className‘, ‘abc‘)
<!DOCTYPE HTML>
<HTML>
<HEAD>
<meta charset="utf-8" />
<title>setAttribute(‘className‘, ‘abc‘)</title>
<style type="text/css">
.abc {
background: red;
}
</style>
</HEAD>
<BODY>
<div id="d1">test div</div>
<script>
var div = document.getElementById(‘d1‘);
div.setAttribute("className", "abc");
</script>
</BODY>
</HTML>
3、el.className = ‘abc‘;
<!DOCTYPE HTML>
<HTML>
<HEAD>
<meta charset="utf-8" />
<title>el.className = ‘abc‘</title>
<style type="text/css">
.abc {
background: red;
}
</style>
</HEAD>
<BODY>
<div id="d1">test div</div>
<script>
var div = document.getElementById(‘d1‘);
div.className = ‘abc‘;
</script>
</BODY>
</HTML>

纯js添加类

标签:class   .class   htm   text   doctype   type   att   set   back   

原文地址:http://www.cnblogs.com/qfdy123/p/7881171.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!