window.onload()函数的使用

    <!-- 行内式的js函数调用,不能用window.onload()函数来包裹,不然页面自上而下执行,扫描到 showBox()函数的时候会报为定义的错误  -->

<!DOCTYPE html>

<html lang="">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title></title>
    <style>
        .btn{
            width:100px;
            height:30px;
            margin-left:10px;
            color:white;
            font-size:16px;
            line-height: 30px;
            text-align: center;
            background-color: #b208e5;
        }
        div{
            width:100px;
            height:100px;
            background-color: deepskyblue;
            margin-top:10px;
            display: none;
        }
        
    </style>
    
</head>


<body>
    <!-- 行内式的js函数调用,不能用window.onload()函数来包裹,不然页面自上而下执行,扫描到 showBox()函数的时候会报为定义的错误  -->
    <button class="btn" οnclick="showBox('1')">show div1</button>
    <button class="btn" οnclick="showBox('2')">show div2</button>
    <button class="btn" οnclick="showBox('3')">show div3</button>
    <button class="btn" οnclick="showBox('4')">show div4</button>
    <div id="box1">1</div>
    <div id="box2">2</div>
    <div id="box3">3</div>
    <div id="box4">4</div>
</body>
</html>
<script>
//        window.onload = function(){
            var box1 = document.getElementById("box1");
            var box2 = document.getElementById("box2");
            var box3 = document.getElementById("box3");
            var box4 = document.getElementById("box4");
            
            function showBox(num1){
//                alert(222);
                switch(num1){
                    case '1':box1.style.display = "block";break;
                    case '2':box2.style.display = "block";break;
                    case '3':box3.style.display = "block";break;
                    case '4':box4.style.display = "block";break;
                    default:break;
                }
            }
//        }
    </script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值