使用DOM修改CSS样式

原创 2019-04-08 22:24:18 473
摘要:<!DOCTYPE html><html><head> <meta charset="utf-8"> <title>改变div案例</title> <style type="text/css"> #box{ width: 100px;     

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>改变div案例</title>

<style type="text/css">

#box{

width: 100px;

        height: 100px;

        background: red;

        margin: 20px 80px;

}

    </style>

</head>

<body>

<!-- 用按钮控制div高度、宽度、颜色、重置、隐藏、显示 -->

<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 type="text/javascript">

    var box

    window.onload=function(){

    box=document.getElementById('box')

    }

    function aa(){

    box.style.height="300px";

    }

    function bb(){

    box.style.width="300px";

    }

    function cc(){

    box.style.background="blue";

    }

    function dd(){

    box.style.height="100px";

    box.style.width="100px";

    box.style.background="red";

    }

    function ee(){

    box.style.display="none";

    }

    function ff(){

    box.style.display="block";

    }

</script>

</body>

</html>

总结:写完样式,一定记得调用,不调用是没有效果的。

批改老师:天蓬老师批改时间:2019-04-09 11:26:55
老师总结:你的代码, 写到了代码块外部了, 还有, 对于只有一行代码的函数, 有必要创建吗/

发布手记

热门词条