H5基础知识第十一课时(className属性)

时间:2021-08-01 20:21:23
<style>
.style1{
width: 200px;
height: 200px;
border: solid 1px red;
}
.style2{
background-color: #00b8e9;
position: absolute;
right: 10px;
}

</style>


<body>
<div class="style1" id="div1">div1</div>


<button id="change">移位</button>


</body>
<script>    var change = document.getElementById("change");    change.addEventListener("click", function () {        var div=document.getElementById("div1");                div.className += " style2";
// className可以设置或返回元素的 class 属性
// div.style.width = "500px";// div.style.backgroundColor = "red"; div.style.cssText="font-size=50px;color=red"/*一定要加上分号*/ });</script>