控制div属性

<!DOCTYPE html>
<html>
    <head>
        <title>控制div属性</title>
        <style type="text/css">
            *{ margin: 0px; padding: 0px; }
            #app1{ width: 300px; height: 160px; margin: 0 auto; }
            #app1 .btng button{ width: 38px; height: 20px; outline: none; }
            .btng{ width: 210px; height: 20px; margin: 10px auto; }
            #bgcolor{ width: 100px; height: 100px; background: black; margin: 20px auto; }
        </style>
    </head>
    <body>
        <div id="app1">
            <div class="btng">
                <button>变宽</button>
                <button>变高</button>
                <button>变色</button>
                <button>隐藏</button>
                <button>重置</button>
            </div>
            <p id="bgcolor"></p>
        </div>

        <script type="text/javascript">
            // 改变样式函数
            var changeStyle = function (elem, attr, value)
                {
                    elem.style[attr] = value;
                };
            window.onload = function(){
                // 获取元素
                var btngcount = document.getElementsByTagName("button");
                var bgcolor = document.getElementById("bgcolor");
                // 储存要更改的元素属性
                var btnAtt = ["width","height","background","display","display"];
                // 储存要更改的值
                var btnVal = ["200px","200px","red","none","block"];
                // 遍历元素
                for(var i = 0; i < btngcount.length; i++){
                    // 令按钮的下标等于i
                    btngcount[i].index = i;
                    // 点击事件
                    btngcount[i].onclick = function(){
                        // 先将样式清空
                        bgcolor.style.cssText = "";
                        // this.index == oBtn.length - 1 && (oDiv.style.cssText = "");
                        changeStyle(bgcolor, btnAtt[this.index], btnVal[this.index]);
                    }
                }
            }
        </script>
    </body>
</html>

                                                      效果图:

                                                           

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值