带有追加的for循环中的jquery错误

时间:2021-01-03 14:28:10

I have a for loop nested in a for loop, where I'm trying to append some dynamically created elements, and I keep getting the error: 'Uncaught TypeError: undefined is not a function', and I'm not sure why. Here's what I have:

我有一个嵌套在for循环中的for循环,我试图附加一些动态创建的元素,并且我一直收到错误:'Uncaught TypeError:undefined不是函数',我不知道为什么。这就是我所拥有的:

HTML:

<div class="particle"></div>

JS:

number_of_particles = 30 + Math.floor(Math.random() * 40);
number_of_layers =  3 + Math.floor(Math.random() * 5);
$particle = $(".particle");

for ( var i = 0; i < number_of_layers; i++ ) {
    var paralax_speed, $particle_layer, $particle_layer_particle;

    paralax_speed = Math.random().toFixed(2);
    $particle_layer = "<div class='particle-layer' data-stellar-ratio='" + paralax_speed + "'></div>";
    for ( var i = 0; i < number_of_particles; i++ ) {
        $particle_layer_particle = "<div class='particle'></div>";
        $particle_layer.append($particle_layer_particle);
    }
    $particle.append($particle_layer);
}

1 个解决方案

#1


2  

You need a jQuery object:

你需要一个jQuery对象:

$particle_layer = $("<div class='particle-layer' data-stellar-ratio='" + paralax_speed + "'></div>");

And two for-loops:

还有两个for循环:

var i, j;
for (i = ...)
    for (j = ...)

#1


2  

You need a jQuery object:

你需要一个jQuery对象:

$particle_layer = $("<div class='particle-layer' data-stellar-ratio='" + paralax_speed + "'></div>");

And two for-loops:

还有两个for循环:

var i, j;
for (i = ...)
    for (j = ...)