jquery查找所有divs孩子的总高度

lor*_*afs 5 javascript jquery jquery-selectors

嘿我有一个包含5个div的div,我想把它们的所有高度加在一起,

这是我最终根据Jeff的回答使用的解决方案.谢谢你的协助.

var ev_totalHeight = 0;
$("#events > div").each(function(){
    ev_totalHeight += $(this).innerHeight();
});



function events_open() {
 $("#events").animate({
"height": ev_totalHeight
  }, 450 );
}

$("#events").click(function() {
events_open();
});
Run Code Online (Sandbox Code Playgroud)

Jef*_*eff 11

这是一个小提琴:http://jsfiddle.net/yj8sL/2/

$(function(){
    var totalHeight = 0;
    $("#parent > div").each(function(){
        totalHeight += $(this).height();
    });
    alert("Total height of all divs: "+totalHeight);
});
Run Code Online (Sandbox Code Playgroud)

如您所见,有5个div,每个高度为100px,因此总高度为500px.

编辑:你的下一个问题(与动画)是你没有告诉它你正在使用什么单位(在你的情况下,像素):

 $("#events").animate({
    "height": ev_totalHeight+"px"
 }, 450 );
Run Code Online (Sandbox Code Playgroud)