开发者

How do I use jQuery to get combined widths of DIVs in a container and pass that value on to another element?

开发者 https://www.devze.com 2023-02-14 07:30 出处:网络
I\'m hoping to use jQuery to get the total widths of all the DIVs in a container and set that width to the body.

I'm hoping to use jQuery to get the total widths of all the DIVs in a container and set that width to the body.

I presume I need something to the effect of

$('#container > div').width().[mulitply by quantity of DIVs in container].[set this result to body width]

Unfortunately just typing strings of what I want isn't good enough! Can someone point me in the right direction?

开发者_StackOverflow社区

Any help appreciated!


This could look like:

var overall_width = 0;

$('#container > div').each(function(index, elem) {
    var $elem = $(elem);
    overall_width += $elem.outerWidth() + parseInt($elem.css('margin-left'), 10) + parseInt($elem.css('margin-right'), 10);
});

$(document.body).width(overall_width);

This would add all the children div node's width along with the margin (if there is any) and finally set the width from document.body.

0

精彩评论

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

关注公众号