循环通过div的jquery

循环通过div的jquery,jquery,loops,Jquery,Loops,希望使用循环来表示此 $("div:nth-child(3)").css({"left": "5px"}); $("div:nth-child(2)").css({"left": "215px"}); $("div:nth-child(1)").css({"left": "425px"}); 如何引用每个 var x=0; $("div").each(function(){ x=x+100; $(this).css('position','absolute'); $(thi

希望使用循环来表示此

$("div:nth-child(3)").css({"left": "5px"});
$("div:nth-child(2)").css({"left": "215px"});
$("div:nth-child(1)").css({"left": "425px"});
如何引用每个

var x=0;
$("div").each(function(){
   x=x+100;
   $(this).css('position','absolute'); 
   $(this).css({"left": "xpx"});  // not sure about this line
  });

这可能是你想要的

$(this).css({"left": x+"px"});
你可以写

$(this).css("left", x);
您不需要添加
px
;jQuery将自动添加它

如果确实要显式添加单元,可以使用字符串串联:

$(this).css("left", x + "px");

尽量不要使用“x”作为变量名,在以后的视场中可能会混淆。修复了您的代码。

如何将
xx
作为一个比
x
更好的变量名?在编程时,使用“x”作为变量名常常会让我感到困惑。这是一个不错的变量名,但我不喜欢使用它。x标记了该点。没有理由,但我大部分时间都使用x开始。然后是y,然后是z。是的,在这个例子中,它确实非常准确。我以前经常使用变量名称,如“x”和“y”,但在编写了数学类软件后就放弃了。我把变量“x”和全局变量“x”或“x-value”混淆了,所以我和我的团队停止使用“x”、“y”、“r”、“s”等作为变量名。习惯;-)你对学习jQuery的书籍或网站有什么建议吗?错。您不需要
(只要不使用大括号使对象成为文字)
var x=0;
$("div").each(function(){
   x = x + 100;
   $(this).css({'position':'absolute', 'left': x + 'px'});
});
var xx=0;
$("div").each(function(){
    xx+=100;
    $(this).css('position','absolute'); 
    $(this).css({"left": xx+"px"}); 
});