javascript - 求大神帮助关于JS中创建元素节点的问题
高洛峰
高洛峰 2017-04-10 17:53:18
[JavaScript讨论组]

我遇到的问题是,通过appendChild创建的元素如果创建多个元素的话,创建的第一个元素有一部份样式没有,不知道去哪里了。如下面是我写的代码。循环创建的p标签 。第一个p标签里面少了top与left这两个样式。求大神帮看一下,是我哪里写错了,还有就是op_x刷新都是0.iaoBox_ge = [0, 0, 0, 0];这个数组里面,无论怎么刷新都是第一个数组有变动,后面的几个数怎么刷新都是0。求大神帮看看我哪里弄错了哈。谢谢。

<!DOCTYPE html>
<html>

    <head>
        <meta charset="utf-8" />
        <title>00015定位式流动布局</title>
        <style>
            * {
                padding: 0px;
                margin: 0px;
            }
            
            #Box {
                height: 500px;
                position: relative;
                
                
            }
            
            .xdsss {
                margin-top: 10px;
            }
        </style>
        <script>
        //获取最小索引值
            function cherkHerigt(op_xiaoBox_ge) {
                var ch_height = 0000000;
                var index = 0;
                for(var i = 0; i < op_xiaoBox_ge.length; i++) {
                    var height_a = op_xiaoBox_ge[i].offsetHeight;
                    if(op_xiaoBox_ge < ch_height) {
                        ch_height = height_a;
                        index = i;
                        
                    };
                    
                };
                return index;
            };

            //
            window.onload = function() {
                var op = document.getElementById('Box');
                //var op_xiaoBox = op.getElementsByTagName('p');
                var Vw = document.documentElement.clientWidth;
                var Vh = document.documentElement.clientHeight;

                var op_xiaoBox_width = 280 + 10; //p盒子的固定宽度+边距
                var op_xiaoBox_num = Math.floor(Vw / op_xiaoBox_width); //Math.floor();向下取整
                
                op.style.width = op_xiaoBox_width * op_xiaoBox_num + "px";//op的总宽度
                op.style.background = "darkred";
                op.style.margin = "0 auto"; //op盒子居中显示

                //初始一个数组/数组对应op_xiaoBox_width的个数
                var op_xiaoBox_ge = [0, 0, 0, 0];
                
                //循环在op里创建p图片容器
                for(var i = 1; i < 8; i++) {
                    var oHeight = Math.ceil(Math.random(300-200)*500); //生成随机数
                    var op_changjian = document.createElement('p');
                    
                    op_changjian.style.top = op_xiaoBox_ge[min_index] + "px";
                    op_changjian.style.left = min_index * op_xiaoBox_width + "px";
                    op_changjian.style.width = 280+10 +"px";
                    op_changjian.style.height = oHeight + "px";
                    op_changjian.style.position = "absolute";
                    op_changjian.style.backgroundColor = "#992288";
                    op_changjian.style.float = "left";
                    
                    //op_changjian.appendChild(oImg);
                    
                    //得到返回的索引值
                    var min_index = cherkHerigt(op_xiaoBox_ge);
                    //alert(min_index);
                    op.appendChild(op_changjian);
                    op_xiaoBox_ge[min_index] += op_changjian.offsetHeight + 8;
                }
            }
        </script>
    </head>

    <body>
        <p id="Box">

        </p>
    </body>

</html>
高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

全部回复(3)
PHPz

谢邀。
不知道你要什么效果,var min_index = cherkHerigt(op_xiaoBox_ge);提前了是可以改变的。

//循环在op里创建p图片容器
  for(var i = 1; i < 8; i++) {
    //得到返回的索引值
    var min_index = cherkHerigt(op_xiaoBox_ge);
    var oHeight = Math.ceil(Math.random(300-200)*500); //生成随机数
    var op_changjian = document.createElement('p');
    op_changjian.style.top = op_xiaoBox_ge[min_index] + "px";
    op_changjian.style.left = min_index * op_xiaoBox_width + "px";
    op_changjian.style.width = 280+10 +"px";
    op_changjian.style.height = oHeight + "px";
    op_changjian.style.position = "absolute";
    op_changjian.style.backgroundColor = "#992288";
    op_changjian.style.float = "left";
    //op_changjian.appendChild(oImg);
    //alert(min_index);
    op.appendChild(op_changjian);
    op_xiaoBox_ge[min_index] += op_changjian.offsetHeight + 8;
}
PHPz

执行第一次循环的时候,op_changjian.style.top = op_xiaoBox_ge[min_index] + "px"; 这句里面的min_index还没有定义,得到的是op_xiaoBox_ge[min_index]得到的是undefined,当然就没法赋值咯

高洛峰

谢邀。
楼上应该是正解了。的确会有min_index为undefined的问题

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

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