简体   繁体   English

使用 jQuery/Javascript 的 for 循环

[英]For loop using jQuery/Javascript

I've created a for loop with javascript/jquery, in order to assign a random number to each div class with the name ".choice".我已经使用 javascript/jquery 创建了一个 for 循环,以便为每个名为“.choice”的 div 类分配一个随机数。 There are four div's in total.总共有四个div。 When I run the following code:当我运行以下代码时:

   for (var i = 0; i < 4; i++) {

    var random = Math.floor(Math.random() * 12);
    var chess = $(".choice");
       chess.attr({
        "data-random": random
       });
       $(".choice").append(chess);
   }  

All divs return the same number, instead of each div .choice having a random number.所有 div 都返回相同的数字,而不是每个 div .choice 都有一个随机数。

(PS first question on the site, hope this question made sense. Thanks in advance.) (PS 网站上的第一个问题,希望这个问题有意义。提前致谢。)

This happens because you're assigning the random number to the class, which assigns the number to every object with that class.发生这种情况是因为您将随机数分配给类,该类将数字分配给具有该类的每个对象。 If you were to console.log your random number, you would see each of your divs ends up with the last random number generated.如果您要 console.log 随机数,您会看到您的每个 div 都以生成的最后一个随机数结束。

Instead, try iterating over your objects with the class by using:相反,请尝试使用该类迭代您的对象:

$('.choice').each(function() {
    //Logic here.
});

On every iteration of the loop, you're assigning the same random number to every element in your selection.在循环的每次迭代中,您都会为选择中的每个元素分配相同的随机数。

This should do it:这应该这样做:

 $(".choice").attr({ "data-random": () => Math.floor(Math.random() * 12) });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="choice">1</div> <div class="choice">2</div> <div class="choice">3</div> <div class="choice">4</div>

You're targeting all div.choice s with var chess = $(".choice");您使用var chess = $(".choice");定位所有div.choice s . . You should move that line outside the for loop and target only the nth div for setting the attribute.您应该将该行移到 for 循环之外,并仅针对第 n 个 div 设置属性。

When you are selecting the element using $(".choice");当您使用 $(".choice"); 选择元素时; this will return you the array of all elements having .choice class.这将返回具有 .choice 类的所有元素的数组。 And when you are adding the attribute it will add the same attribute with same value everytime.当您添加属性时,它每次都会添加具有相同值的相同属性。

It is taking the last random value which was generated in the last ie.它采用最后一个 ie 中生成的最后一个随机值。 4th iteration.第 4 次迭代。

var chess = $(".choice");
for (var i = 0; i < 4; i++) {
    var random = Math.floor(Math.random() * 12);
    chess[i].attr({ "data-random": random });
}

I ran this loop till 4th element as asked in the question.我按照问题的要求运行了这个循环,直到第 4 个元素。

If you want to run this for all elements having same class name as .choice you should run it as below :-如果你想为所有与 .choice 具有相同类名的元素运行它,你应该按如下方式运行它:-

var chess = $(".choice");
for (var i = 0; i < chess.length; i++) {
    var random = Math.floor(Math.random() * 12);
    chess[i].attr({ "data-random": random });
}

This will add the random number for each element having classname choice for attribute data-random.这将为每个具有属性 data-random 的类名选择的元素添加随机数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM