使用jQuery获得树效果

使用jQuery获得树效果,jquery,Jquery,我有这个密码 $('.children a:first').css({'top':'0px','left':'100px'}); $('.children a:eq(1)').css({'top':'20px','left':'120px'}); $('.children a:eq(2)').css({'top':'40px','left':'140px'}); $('.children a:last').css({'top':'60px','left':'160px'}); 是否可以将其更改

我有这个密码

$('.children a:first').css({'top':'0px','left':'100px'});
$('.children a:eq(1)').css({'top':'20px','left':'120px'});
$('.children a:eq(2)').css({'top':'40px','left':'140px'});
$('.children a:last').css({'top':'60px','left':'160px'});

是否可以将其更改为函数并指定偏移量?试图弄清楚我是否可以做一些事情来循环.children下的每个a元素,并将顶部和左侧属性加倍。

下面是一个工作示例:

这里唯一的技巧是我们传递到匿名函数中的i。每个。我将从0开始,每次循环开始时增加1。每个循环开始另一次迭代。从这里开始,这只是基本的数学。

您不能使用嵌套的uls来实现树结构吗?
$('.children a').each(function(i){
    $(this).css({top: i*20, left: i*20 + 100});
});