我想用所有子元素(.project)之和来计算父元素(#projects)的宽度。我试过jQuery,但它不起作用,-也许有人能帮我?
干杯
var reset = 0;
var width = $(".project").each(function() { reset += $(this).width(); });
$(window).on( "resize", function () {
$("#projects").css("width", width);
}).resize();发布于 2014-08-20 16:29:32
$(window).on("resize", function () {
var width = 0;
$('.project').each(function() {
width += $(this).outerWidth( true );
});
$('#projects').css('width', width);
}).resize();这应该能行。
发布于 2014-08-20 16:31:11
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(); // handlerhttps://stackoverflow.com/questions/25409750
复制相似问题