JS---设置简易红绿灯


<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    /*设置原始div大小*/
    <style>
        #div1{
            width: 100px;
            height: 100px;
        }
    </style>
</head>
<body onload="changered()">  <!--onload=""是body里的属性,用于调方法-->
    <div id="div1"></div>   
</body>
<!--setTimeout()表示一次性定时器-->
<script>
    setTimeout(changered,0);    /*设置最开始时间间隔为0*/
    function changered(){
        var div=document.getElementById("div1");
        div.style.backgroundColor="red";
        setTimeout(changeyellow,7000);/*红灯持续亮7秒*/

    }
    function changeyellow(){
        var div=document.getElementById("div1");
        div.style.backgroundColor="yellow";
        setTimeout(changegreen,3000);

    }
    function changegreen(){
        var div=document.getElementById("div1");
        div.style.backgroundColor="green";
        setTimeout(changered,5000);

    }

</script>
</html>

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM