jquery查找所有divs孩子的总高度 [英] jquery finding the total height of all divs children
本文介绍了jquery查找所有divs孩子的总高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嘿我有一个包含5个div的div,我想将它们的所有高度加在一起,
hey I have a div with 5 div's contained in it, I want to add all of their heights together,
这是我最终使用的解决方案杰夫的回答谢谢你帮帮我。
This is the solution I ended up using based off of Jeff's answer. Thanks for helping me out.
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();
});
推荐答案
这是一个小提琴:http://jsfiddle.net/yj8sL/2/
$(function(){
var totalHeight = 0;
$("#parent > div").each(function(){
totalHeight += $(this).height();
});
alert("Total height of all divs: "+totalHeight);
});
如您所见,有5个div,每个高度为100px,因此总高度为500px。
As you see, there are 5 divs, with a height of 100px each, so the total height is 500px.
编辑:您的下一个问题(使用动画)是您没有告诉它您使用的是哪个单位(在您的情况下,像素):
Your next problem (with the animate) is that you are not telling it what unit you are using (in your case, pixels):
$("#events").animate({
"height": ev_totalHeight+"px"
}, 450 );
这篇关于jquery查找所有divs孩子的总高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文