gpt4 book ai didi

javascript - .addClass 错过了在 JavaScript 中使用 for 循环的最终动态创建的元素?

转载 作者:行者123 更新时间:2023-12-03 02:12:48 25 4
gpt4 key购买 nike

我正在尝试使用 for 循环和 .addClass 与 jquery 向动态创建的 div 添加一个唯一的类。循环成功地将唯一类添加到所有创建的 div,除了最终迭代(在这种情况下为 .square143)。

for 循环还向所有这些动态创建的元素 (.art) 添加一个统一的类,并且做得很成功。

我遇到的另一个问题(可能与此相关)是该程序创建的 div 是预期的 div 的两倍。

基本上,我试图向 for 循环创建的最终迭代 div 添加一个唯一的类。

function addElementEllsworth () {

//For loop will dynamically create specified number of empty divs
for (var i = 0; i < 144; i++) {

//Actually creating divs here
var newDiv = document.createElement("div");
var currentDiv = document.getElementById("div1");

//Giving all the created divs for ability to change CSS of entire grid
$(function() {
$("div").addClass("art");
});

//Giving each individual div a unique class. Then assigning a random color (RGB value) to that class using function.
$("div").each(function(i) {
$(this).addClass("square" + i);
$(this).css('backgroundColor', randomEllsworthColor());
});

//Putting them into the body of the file
document.body.insertBefore(newDiv, currentDiv);

}

} addElementEllsworth ();

//This function will return a random color (RGB value). The function forms the return value by assembling the proper RGB syntax and random numbers created in a different function.
function randomColor () {

var maxRGBValue = 255;

var r = generateRandom(maxRGBValue);
var g = generateRandom(maxRGBValue);
var b = generateRandom(maxRGBValue);

var theColor = "rgb(" + r + "," + g + "," + b + ")";

return theColor;
}


//Returns a hex value associated with Kelly's Spectrum painting at the SFMOMA
function randomEllsworthColor () {

//Color hexes taken from the Ellsworth Kelly painting at the SFMOMA
ellsworthColors = ["#2f2d2d","#c6becd","#ff8635","#3b354c","#94d35a","#f7f25e","#0170c1","#243881","#703550","#b38cb9","#7bc653","#do2624","#f2a00f","#f3e44e"];

//Function returns a value from the above array. Index is randomly selected by generating a random index from the array.
return ellsworthColors[generateRandom(ellsworthColors.length)];
}

function generateRandom (num) {

return Math.floor(Math.random() * Math.floor(num));
}
.art {

float:left;
width: 08.33333333%;
padding-bottom: 08.333333%; /* = width for a 1:1 aspect ratio */
margin:0%;
background-color: cyan; /* commenting will hide all colorless square */

}
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<link rel="stylesheet" href="ellsworth.css">
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>

最佳答案

最后一个 div 没有您期望的类的原因是 $("div").each(function(i) { 选择页面上已有的 div,当在您的代码中直到 document.body.insertBefore 之前,最新的 div 尚未添加到页面。

我创建了一支笔来展示您的代码在没有 JQuery 的情况下的外观,即使用纯 Javascript。

https://codepen.io/theleebriggs/pen/XEeMve

希望有帮助。

关于javascript - .addClass 错过了在 JavaScript 中使用 for 循环的最终动态创建的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49482133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com