JavaScript改变DIV的样式

原创 2019-03-31 21:57:14 361
摘要:<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>changeDIV</title> <style>         #box
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>changeDIV</title>
<style>
        #box{
         width:100px;
         height:100px;
         background:red;
         margin:20px 80px;
        }
</style>
</head>
<body>
<div id="box"></div>
<input type="button" value="变高" onclick="aa()">
<input type="button" value="变宽" onclick="bb()">
<input type="button" value="变色" onclick="cc()">
<input type="button" value="重置" onclick="dd()">
<input type="button" value="隐藏" onclick="ee()">
<input type="button" value="显示" onclick="ff()">

<script>
        var box
        window.onload=function(){
         box=document.getElementById("box");
        }
        function aa(){
         box.style.height="400px";
        }
        function bb(){
         box.style.width="400px";
        }
        function cc(){
         box.style.background="blue";
        }
        function dd(){
         box.style.width="100px";
         box.style.height="100px";
         box.style.background="red";
        }
        function ee(){
         box.style.display="none";
        }
        function ff(){
         box.style.display="block";
        }
</script>
</body>
</html>

总结:在对网页的布局中,如果想要对DIV的样式做出改变,可以运用JS来改变DIV中的样式,在本节课中,我学到了如何运用JS来改变DIV的样式,声明的变量.style.属性名="属性值",如果属性有“-”号,就写成驼峰形式。要多加练习,好好掌握,深入理解。

批改老师:天蓬老师批改时间:2019-04-01 10:09:59
老师总结:有空了解一下classList, 它现在已可以代替掉className了

发布手记

热门词条