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