jQuery find width of each li element?
anyone know a way to find the width of each li within the ul?
I tried
var totalWidth;
jQuery("ul li").each(function() { totalWidth += jQuery(this).outerWidth(); });
But didn't work..开发者_如何学JAVA.
You need to initialize the variable first:
var totalWidth = 0;
...otherwise you're starting out effectively doing:
undefined += jQuery(this).outerWidth();
...which results in NaN
for subsequent iterations:
NaN += jQuery(this).outerWidth();
Initiate the variable as 0. Your code seems like it should work
精彩评论