我有一个嵌套在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);
}
答案 0 :(得分:2)
你需要一个jQuery对象:
$particle_layer = $("<div class='particle-layer' data-stellar-ratio='" + paralax_speed + "'></div>");
还有两个for循环:
var i, j;
for (i = ...)
for (j = ...)