控制元素样式
<body>
<div id="div1">
div
</div>
<script>
var div1 = document.getElementById("div1");
div1.onclick = function(){
div1.style.border = "1px solid red";
div1.style.height = "200px";
div1.style.width = "200px";
div1.style.fontSize = "100px";
}
</script>
</body>
- 2.提前定义好类选择器class的样式,使用元素的className属性来设置元素class的属性值
<style>
.d1{
border: 1px solid red;
width: 100px;
height: 100px;
}
.d2{
border: 1px solid blue;
width: 200px;
height: 200px;
}
</style>
<body>
<div id="div2">
div2
</div>
<script>
var div2 = document.getElementById("div2");
div2.onclick = function(){
div2.className = "d2";
}
</script>
</body>