javascript 如何使用jQuery在for循环的迭代中不断添加到变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3793016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 02:15:35  来源:igfitidea点击:

How to continuously add to a variable on iteration of a for-loop, with jQuery?

javascriptjqueryfor-loop

提问by David says reinstate Monica

I'm trying to increase a number on each iteration of a forloop, in jQuery (1.4.2), by the width of the previous element.

我试图for在 jQuery (1.4.2) 中通过前一个元素的宽度在循环的每次迭代中增加一个数字。

I've tried the following:

我尝试了以下方法:

var
$lis = $('#bookmarks > li'),
liHeight = parseInt($lis.height()),
numLis = $lis.length;
console.log(numLis);

var totalLeft = '0';
console.log(totalLeft);

for (i=1; i<numLis; i++) {
    var leftOffset = $lis.eq(i-1).width();
    var leftTotal = leftOffset + leftTotal;


    console.log(leftOffset +"/"+ leftTotal);
}

The output from this section is:

本节的输出是:

11 (the length of the array)
0 (the initial value of 'totalLeft')
97/97
117/214
90/
115/NaN
101/NaN
138/NaN
93/NaN
96/NaN
102/NaN
80/NaN

I've tried using parseInt()around one, and both, variables in the var leftTotal = leftOffset + leftTotal;variable assignment, to no avail. I've also tried using jQuery's each(), with the exact same result. Which is unsurprising, since I assigned the values in almost exactly the same way...

我试过parseInt()var leftTotal = leftOffset + leftTotal;变量赋值中使用一个和两个变量,但无济于事。我也试过使用 jQuery's each(),结果完全相同。这并不奇怪,因为我以几乎完全相同的方式分配值......

There are two questions here:

这里有两个问题:

  1. Why is leftTotalnot-a-number (NaN)?
  2. How can I add the newvalue of leftOffsetto the previous-iteration's value of leftOffset?
  1. 为什么leftTotal不是数字 ( NaN)?
  2. 如何将 的值添加leftOffset到前一次迭代的 值leftOffset

The console log shouldread something like:

控制台日志应该是这样的:

11
0
97/97
117/214
90/304
115/419
101/520
138/658
93/751
96/847
102/949
80/1029



Edited针对@KennyTM 进行了编辑

Console.log output is now (more promising):

Console.log 输出现在是(更有希望):

11
0
97 "/" "970"
117 "/" "117970"
90 "/" "90117970"
115 "/" "11590117970"
101 "/" "10111590117970"
138 "/" "13810111590117970"
93 "/" "9313810111590117970"
96 "/" "969313810111590117970"
102 "/" "102969313810111590117970"
80 "/" "80102969313810111590117970"

With regards to @Tomalak: yeah, it was a typo. Sadly it was a typo in both my code here and in the real darn script. ...sigh... Thanks for the catch, though, that seems to have done a lot to help out.

关于@Tomalak:是的,这是一个错字。遗憾的是,我在这里的代码和真正的该死的脚本中都有一个错字。......叹气......不过,感谢您的捕获,这似乎已经做了很多帮助。

...how embarrassing. =)

...多么尴尬。=)

采纳答案by Tomalak

It appears that you are not assigning to totalLeftin your code.

看来您没有totalLeft在代码中分配给。

Also, I think your code is wayto complicated and has subtle errors. Here is a more compact and jQuery-style version.

另外,我觉得你的代码是这样复杂的,有细微的错误。这是一个更紧凑和 jQuery 风格的版本。

var totalLeft = 0;

$('#bookmarks > li:gt(0)').prev().each(function () {
    totalLeft += $(this).width();
});

At least, this generates the same number as the code in your own answer.

至少,这会生成与您自己的答案中的代码相同的数字。

回答by someGuy

i'd try somethin like this:

我会尝试这样的事情:

var leftTotal = 0
$('#bookmarks > li').each( function(){ 
    leftTotal += parseInt(this.width());  // just to be sure its an int :) 
    console.log(this.width(); +"/"+ leftTotal);
});

回答by Roman

It seems you are not keeping the last value for the next iteration. i.e - put " var leftTotal" out of the loop, so it will keep the value for the next iteration and you'll add to it.

您似乎没有保留下一次迭代的最后一个值。即 - 将“var leftTotal”置于循环之外,因此它将保留下一次迭代的值,您将添加到它。

Hope I got you right :)

希望我说对了:)

回答by Praveen Prasad

var totalLeft = 0;
console.log(totalLeft);

for (i=1; i<numLis; i++) {
    var leftOffset = $lis.eq(i-1).width();
    var leftTotal = parseInt(leftOffset,10) + totalLeft;


    console.log(leftOffset +"/"+ leftTotal);
}

回答by David says reinstate Monica

The solution was, apparently, to pay a little more attention to variable names, and use a parseInt:

显然,解决方案是多注意变量名称,并使用 parseInt:

var
$lis = $('#bookmarks > li'),
liHeight = parseInt($lis.height()),
numLis = $lis.length;
console.log(numLis);

var leftTotal = '0';    // somehow, between creating this var and the loop I started calling it something else 'totalLeft', for no known reason.
console.log(leftTotal);

for (i=1; i<numLis; i++) {
    var leftOffset = $lis.eq(i-1).width();
    var leftTotal = leftOffset + parseInt(leftTotal);   // added the parseInt here to force it to be a number.      

    console.log(leftOffset, "/" , leftTotal);
}

Thanks to @Tomalak who caught my clinical blindness... =)

感谢@Tomalak 发现了我的临床失明... =)