搜索
javascript - js的for循环改变样式结构
巴扎黑
巴扎黑 2017-04-10 16:24:11
[JavaScript讨论组]

先放出代码

<!DOCTYPE html>
<html lang="en">
<head>

    <meta charset="UTF-8">
    <title></title>
<script>
    window.onload = function(){
        var body = document.getElementsByTagName('body')[0];
        var btn = body.getElementsByTagName('button');
        for(var num = 0;num < 40;num++){
        body.innerHTML += "<button>" + "按钮" +"</button>";
    }
        for(var num = 0;num < 20;num++){
        //btn[num].style.marginLeft = "15px";
        btn[num].style.position = "absolute";
        btn[num].style.top = num*30 + "px";
        btn[num].style.left = num*30 + "px";
        }

        for(var num = 20;num < 40;num++){
        //btn[num].style.marginLeft = "15px";
        btn[num].style.position = "absolute";
        btn[num].style.top = num*30 + "px";
        btn[num].style.left = num*30 + "px";
        }
    };
</script>

</head>
<body>

</body>
</html>

希望按钮从num=21开始不断上升,然后与之前的按钮形成一个V字

巴扎黑
巴扎黑

全部回复(3)
PHPz

代码如下:

<!DOCTYPE html>
<html lang="en">
    <head>
    <meta charset="UTF-8">
    <title></title>
     </head>
     <body>

     </body>
</html>
<script>
    window.onload = function(){
        var body = document.getElementsByTagName('body')[0];
        var btn = body.getElementsByTagName('button');
        for(var num = 0;num < 40;num++){
        body.innerHTML += "<button>" + "按钮" + num + "</button>";
    }
        for(var num = 0;num <= 20;num++){
            //btn[num].style.marginLeft = "15px";
            btn[num].style.position = "absolute";
            btn[num].style.top = num*30 + "px";
            btn[num].style.left = num*30 + "px";
        }

        for(var num = 21;num < 40;num++){
            //btn[num].style.marginLeft = "15px";
            btn[num].style.position = "absolute";
            btn[num].style.top = btn[40-num].style.top;
            btn[num].style.left = num*30 + "px";
        }
    };
</script>
巴扎黑
<!DOCTYPE html>
<html lang="en">
<head>

    <meta charset="UTF-8">
    <title></title>
    <script>
        window.onload = function(){
            var body = document.getElementsByTagName('body')[0];
            var btn = body.getElementsByTagName('button');

            var totalButtongLength=11;
            for(var num = 0;num < totalButtongLength;num++){
                body.innerHTML += "<button>" + "按钮" +num+"</button>";
            }
            var half=parseInt(totalButtongLength/2);
            var top;
            if(totalButtongLength%2!=0){
                half=parseInt(totalButtongLength/2);
                top=half;
            }

            for(var num = 0;num<totalButtongLength;num++){
                if(num<half){
                    btn[num].style.position = "absolute";
                    btn[num].style.top = num*30 + "px";
                    btn[num].style.left = num*30 + "px";
                }else{
                    btn[num].style.position = "absolute";
                    btn[num].style.top = (totalButtongLength-1-num)*30 + "px";
                    btn[num].style.left = num*30 + "px";
                }
                if(top&&num==top){
                    btn[top].style.position = "absolute";
                    btn[top].style.top = top*30 + "px";
                    btn[top].style.left = top*30 + "px";
                }

            }


        };
    </script>
</head>
<body>

</body>
</html>
PHPz
function _SetCSSStyleValue(css,clsname,style,value){
        var i;
        var rules;
        if (css.rules) rules = css.rules;
        else rules = css.cssRules;
        for (i = 0; i < rules.length; i++){
            if (rules[i].selectorText === "." + clsname){
                var s = "rules[" + i + "].style." + style + " = \"" + value + "\"";
                eval(s);
                break;
            }
        }
    }
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号