jQuery属性问题

Sho*_*911 0 jquery attributes

我正在尝试一次选择一系列div.我确信这是一个更好的方法,但我给了他们所有独特的rels,我正在使用rels一次选择一个.我想将它们移动到div的宽度上,然后在屏幕上放下动画.我有的是:

    $(document).ready(function(){
        var totalbricks = 7;
        var index = 2;
        $(".brick").animate({top:"600px"});
        while(index < totalbricks)
        {
            var postion = 45*index;
            $(".brick[rel='+index+']").css('left', postion+'px');
            $(".brick[rel='+index+']").animate({top:"600px"});
            index++;
        }
 });
Run Code Online (Sandbox Code Playgroud)

所有div都在容器div中.
Jquery文档说,'变量可以使用以下语法使用:[name ='"+ MyVar +"']'
我做错了什么?


这是jQuery使用的HTML

    <body>
<div id="container">
    <div id="canvas">
        <div class="brick" rel="1">
        </div>
        <div class="brick" rel="2">
        </div>
        <div class="brick" rel="3">
        </div>
        <div class="brick" rel="4">
        </div>
        <div class="brick" rel="5">
        </div>
        <div class="brick" rel="6">
        </div>
        <div class="brick" rel="7">
        </div>

    </div>
</div>

</body>
Run Code Online (Sandbox Code Playgroud)

Nic*_*rdi 6

你在JavaScript中混合'和'

    $(document).ready(function(){
            var totalbricks = 7;
            var index = 2;
            $(".brick").animate({top:"600px"});
            while(index < totalbricks)
            {
                    var postion = 45*index;
                    $(".brick[rel="+index+"]").css('left', postion+'px');
                    $(".brick[rel="+index+"]").animate({top:"600px"});
                    index++;
            }
    });
Run Code Online (Sandbox Code Playgroud)

试着注意到.brick[rel=我用的是双引号而不是单引号.


更新

您还可以使用该each功能执行以下操作,这可能使您更容易

$(document).ready(function() {
    var bricks = $(".bricks");
    bricks.animate({ top: "600px" });

    bricks.find(":not(:first)").each(function(i) {
        var position = 48 * (i + 1);
        $(this).css("left", position + "px");
        $(this).animate({ top: "600px" });
    });
}
Run Code Online (Sandbox Code Playgroud)

这是使用更"jQuery"方式完成同样事情的相同方法.