我想用所有子元素(.project)的总和来计算父元素(#projects)的宽度。我尝试过jQuery但它不起作用 - 也许有人可以帮我解决?
干杯
var reset = 0;
var width = $(".project").each(function() { reset += $(this).width(); });
$(window).on( "resize", function () {
$("#projects").css("width", width);
}).resize();
答案 0 :(得分:0)
$(window).on("resize", function () {
var width = 0;
$('.project').each(function() {
width += $(this).outerWidth( true );
});
$('#projects').css('width', width);
}).resize();
这应该有用。
答案 1 :(得分:0)
var projects = $("#projects"),
project = projects.find(".project"), // just caching the dom for performance
setWidth = function() { // you want is as a function
var totalWidth = 0,
project.each(function() {
totalWidth += $(this).width();
});
projects.width(totalWidth);
};
$(window).on("resize", setWidth).resize(); // handler