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


I am trying to increase a number on each iteration in jQuery, for loop (1.4 .2), from the width of the previous element.

I have tried the following:

  var $ lis = $ ('# bookmark & ​​gt; li'), LiHeight = parseInt ($ lis.height () ), NumLis = $ lis.length; Console.log (numLis); Var totalLeft = '0'; Console.log (totalLeft); (Left-leftOffset = $ lis.eq (i-1) .width (); Var leftTotal = leftOffset + leftTotal; Console.log (leftOffset + "/ (for left I = 1; i & lt; numLis; i ++) "+ Byte total);}  

The output of this section is:

  11 (the length of the array) 0 (the initial value of 'total left) 97/97 117/214 90/115 / nan 101 / nan 138 / nan 93 / nanon 969 / nan n 102 / nan 80 / nan  

I have tried ParseInt () one around, and both, var leftTotal = leftOffset + variables in the leftTotal; Convertible assignment, there is no benefit. I also have jQuery each < Use of code> I have tried to c, with the exact same result. Which is uncertain, since I have assigned almost the same values ​​...

Here are two questions:

  1. Why leftTotal non-A-number ( NaN )
  2. I new leftOffset leftOffset > 0 97/97 117/214 90/304 115/419 101/520 138/658 93/751 96/847 102/949 80/1029


    edit

    / P>

    Console.log out Put is now (more promising):

      11 9 7 / "970" 117 "/" "117970" 90 "/" "90117970" 115 "/" 11590117970 "101" / "10111590117970" 138 "/" "13810111590117970" 93 "/" "9313810111590117970" 96 "/" 969313810111590117970 "102" / "" 102969313810111590117970 "80" / "" 80102969313810111590117970 "Regarding @Tomalak: Yes, it was a typo . Sadly it was a typo in both my code and in the actual darn script. ... ah ... thanks for catching, though, it seems that there is a lot to help.  

    ... how embarrassing =)

    It appears that you In the code, totalLeft are not specified.

    In addition, I think your code is complex as the way and there are subtle errors in it. Here's a more compact and jQuery-style version.

      var totalLeft = 0; $ ('# Bookmarks'; li: gt (0)') Previous (). Each (function () {totalLeft + = $ (this). Width ();});  

    At least, it generates the same number.


Comments