开发者

jQuery find width of each li element?

开发者 https://www.devze.com 2023-03-29 02:32 出处:网络
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(); });

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

0

精彩评论

暂无评论...
验证码 换一张
取 消