jQuery:DIV 填充视口剩余部分的高度计算

jQuery: Height calculation for DIV fill remainder of viewport

我正在尝试计算 window 的剩余高度减去所有其他页面元素的高度,以便我可以将所述高度分配给 DIV。我想我的结构是正确的,我只是不确定语法。

$('#container').height($(window).height() - $('#a, #b, #c, #d').height());

https://jsfiddle.net/0gtkgLem/4/

我认为你的语法是正确的。你的 jsfiddle 没有工作,因为你没有包含 jQuery 插件。参见 https://jsfiddle.net/0gtkgLem/1/

jQuery的.height()只得到你select的元素集合中第一个元素的高度。所以你需要 select 每个单独的并自己得到它的高度。

来自文档:

Get the current computed height for the first element in the set of matched elements or set the height of every matched element.

http://api.jquery.com/height/

$('#container').height($(window).height() - $('#a').height() - $('#b').height() - $('#c').height() - $('#d').height());

已更新 Fiddle:https://jsfiddle.net/0gtkgLem/2/