javascript - Jquery 使递归 div 成为可能?

标签 javascript jquery

不确定如何执行此操作,但我正在尝试使 div 的行为类似于在屏幕上均匀拉伸(stretch)的列(这已完成/很容易),然后制作子列。这是我所拥有的: JS:

$(function() {
    cols = $('.column');
    parent_width = cols.parent().width();
    col_fluff = parseInt(cols.css('padding-left'))+parseInt(cols.css('padding-right'))+parseInt(cols.css('margin-left'))+parseInt(cols.css('margin-right'));
    col_width = (Math.floor(parent_width/cols.size()-col_fluff));

    cols.each(function(){
        $(this).width(col_width);
    });
});

CSS:

#container{
    position:relative;
    width:100%;
    height:100%;
    margin-top:50px;
}
.column{
    float:left;
    top:0px;
    left:0px;
    margin-right:20px;
    outline:#000 solid 2px;
    width:20%;
}
.clear{
    clear:both;
}

HTML:

<div id="container">
    <div class="column">
    This is some text : This is some text : This is some text : This is some text :This is some text : This is some text :This is some text : This is some text :This is some text : This is some text :This is some text : This is some text :This is some text : This is some text :This is some text : This is some text :This is some text : This is some text 
    </div>
    <div class="column">
    This is some text : This is some text : This is some text : This is some text :This is some text : This is some text :This is some text : This is some text :This is some text : This is some text :This is some text : This is some text :This is some text : This is some text :This is some text : This is some text :This is some text : This is some text 
    </div>
    <div class="clear">clear</div>
</div><!-- end container -->

在您尝试插入内部列之前,这一切正常:

<div id="container">
    <div class="column">
    This is some text : This is some text : This is some text : This is some text :This is some text : This is some text :This is some text : This is some text :This is some text : This is some text :This is some text : This is some text :This is some text : This is some text :This is some text : This is some text :This is some text : This is some text 
        <div class="column">
            inner column
        </div>
        <div class="column">
            inner column
        </div>
    </div>
    <div class="column">
    This is some text : This is some text : This is some text : This is some text :This is some text : This is some text :This is some text : This is some text :This is some text : This is some text :This is some text : This is some text :This is some text : This is some text :This is some text : This is some text :This is some text : This is some text 
    </div>
    <div class="clear">clear</div>
</div><!-- end container -->

有什么想法吗?

最佳答案

您可以按级别从外列到内列排列列宽:

定义一个函数,它接受包含 $('.column') 元素的最外层父元素并安排它的直接子元素,然后将相同的函数应用于每个 $('.column' ) child (现在作为 parent ,安排它的 child )递归...

$(function(){
    function Arrange(colsParent){

        cols = colsParent.children('.column');
        parent_width = cols.parent().width();
        col_fluff = parseInt(cols.css('padding-left'))
                   +parseInt(cols.css('padding-right'))
                   +parseInt(cols.css('margin-left'))
                   +parseInt(cols.css('margin-right'));
        col_width = (Math.floor(parent_width/cols.size()-col_fluff));

        cols.each(function(){
            $(this).width(col_width);
        });


        cols.each(function(){
            Arrange($(this));
        });
    }

    level1ColsParent = $('.column').first().parent();

    Arrange(level1ColsParent);
});

关于javascript - Jquery 使递归 div 成为可能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5825403/

相关文章:

javascript - 无法正确更新 React 状态变量

javascript - 更改前置文件输入的位置

javascript - 删除或模糊水平滚动条上的线条?

javascript - 流过当前页面的页面

javascript - 如何配置 jsbeautifier 设置以实际美化我的 HTML/Javascript 代码

javascript - 我想要一个拼写检查器插件到 html 可编辑 div 中吗?

javascript - 我应该将 jQuery 或 DOM 对象作为参数传递吗? (性能问题)

jquery - 在内容级别与 google chrome 扩展一起使用时,未定义 DP_jQuery_*

javascript - jquery/userscript 未定义?

javascript - node.js child_process.spawn 没有标准输出,除非 'inherit'