Javascript 数组中的jquery对象未定义

Javascript 数组中的jquery对象未定义,javascript,jquery,html,Javascript,Jquery,Html,我正在创建大量随机div并将它们附加到主体中: var cubes = [], allCubes = '' for(var i = 0; i < 150; i++) { var randomleft = Math.floor(Math.random()*1000), randomtop = Math.floor(Math.random()*1000); allCubes += '<div id="cube'+i+'" style="posit

我正在创建大量随机div并将它们附加到主体中:

var cubes    = [], 
allCubes  = ''
for(var i = 0; i < 150; i++) {
    var randomleft = Math.floor(Math.random()*1000),
        randomtop = Math.floor(Math.random()*1000);
    allCubes += '<div id="cube'+i+'" style="position: absolute; border: 2px #000 solid; left: '+randomleft+'px; top: '+randomtop+'px; width: 15px; height: 15px;"></div>';
    cubes.push($('#cube'+i));
}
$('body').append(allCubes);

然后我抛出了
未定义的
。为什么?

因为您只是在稍后的时间点添加元素,所以您正在使用选择器在添加之前创建一个jquery对象,该选择器将不获取任何内容(因为元素还不在DOM中)。相反,只需将id保存在数组中,然后构建对象

var cubes    = [], 
allCubes  = ''
for(var i = 0; i < 150; i++) {
    var randomleft = Math.floor(Math.random()*1000),
        randomtop = Math.floor(Math.random()*1000);
    allCubes += '<div id="cube'+i+'" style="position: absolute; border: 2px #000 solid; left: '+randomleft+'px; top: '+randomtop+'px; width: 15px; height: 15px;"></div>';
    cubes.push('#cube'+i); //just save the id
}
$('body').append(allCubes);
或者:

    var cubes  = []; 
    for(var i = 0; i < 150; i++) {
        var randomleft = Math.floor(Math.random()*1000),
            randomtop = Math.floor(Math.random()*1000);
        cubes.push($('<div id="cube'+i+'" style="position: absolute; border: 2px #000 solid; left: '+randomleft+'px; top: '+randomtop+'px; width: 15px; height: 15px;"></div>'));
    }
    $('body').append(cubes);

PSL是正确的。或者每次只追加代码

randomtop = Math.floor(Math.random()*1000);
$('body').append('<div id="cube'+i+'" style="position: absolute; border: 2px #000 solid; left:    '+randomleft+'px; top: '+randomtop+'px; width: 15px; height: 15px;"></div>');
cubes.push($('#cube'+i));
randomtop=Math.floor(Math.random()*1000);
$('body')。追加('');
push($('#cube'+i));

您正在尝试创建:

cubes.push($('#cube'+i));
#cubeX
项在DOM中之前,因此您创建并放入数组的jQuery对象中没有任何内容,因为在您创建jQuery对象时,它无法在DOM中找到
#cubeX

我建议您在添加的项上放置一个公共类,并将代码更改为:

allCubes  = ''
for(var i = 0; i < 150; i++) {
    var randomleft = Math.floor(Math.random()*1000),
        randomtop = Math.floor(Math.random()*1000);
    allCubes += '<div id="cube'+ i + '" class="cube" style="position: absolute; border: 2px #000 solid; left: '+randomleft+'px; top: '+randomtop+'px; width: 15px; height: 15px;"></div>';
}
$('body').append(allCubes);
您不需要将它们永久存储在阵列中。需要的时候就去拿

如果要禁用第二个属性,可以执行以下操作:

$('#trigger').click(function() { 
     console.log($(".cube").eq(1).attr('id'));
});

@超级大小“是”,因为您每次单击都要创建它,如果不是,您可以做的是获取所有立方体。。。使用以选择器开始。感谢您的编辑。但是为了清楚起见,我不能用
cubes.push($('#cube'+i))附加所有多维数据集后?@supersize不需要。请参阅我的第二个示例和演示。这是在一个地方做的不不不。。最好在最后附加到DOM,而不是在每次迭代期间。
cubes.push($('#cube'+i));
allCubes  = ''
for(var i = 0; i < 150; i++) {
    var randomleft = Math.floor(Math.random()*1000),
        randomtop = Math.floor(Math.random()*1000);
    allCubes += '<div id="cube'+ i + '" class="cube" style="position: absolute; border: 2px #000 solid; left: '+randomleft+'px; top: '+randomtop+'px; width: 15px; height: 15px;"></div>';
}
$('body').append(allCubes);
$(".cube")
$('#trigger').click(function() { 
     console.log($(".cube").eq(1).attr('id'));
});