简体   繁体   English

将Div Height设置为另一个Div Height

[英]Set Div Height to Another Div Height

I know similar questions have been asked and I have referenced them, but have yet to get this to work. 我知道有人问过类似的问题,我已经引用了它们,但尚未使它生效。 I have an aside that may not have as much content as the main section next to it. 我的一旁可能没有旁边的主要内容那么多。 I am using the load event window.load to run my script called asideHeightSet below is the snippet that calls it: 我正在使用load事件window.load运行名为asideHeightSet的脚本,下面是调用它的代码段:

  <script>
    window.onload=function(){

        asideHeightSet("main_content_aside", "main_content_section");
    }
  </script>

I have other functions that are executing fine from there and this snippet is after I can any dependent jQuery libraries. 我还有其他可以从那里执行的功能,并且可以在任何依赖的jQuery库之后使用此代码段。 My javascript for the asideHeightSet is: 我的asideHeightSet的JavaScript是:

var $ = function (id) { return document.getElementById(id); }
function asideHeightSet(aside, section){
var sectionHeight = $(section).style.height;
$(aside).style.height= sectionHeight;

}

I ran the firebug debugger in firefox and it keeps on showing that the height of the section is blank it outputs "" in the watch when I step through. 我在firefox中运行了firebug调试器,并不断显示该部分的高度为空白,当我单步执行时在手表中输出“”。

Any help would be appreciated. 任何帮助,将不胜感激。

Thx 谢谢


This did it for me: 这为我做到了:

function asideHeightSet(aside, section){ var sectionHeight = $(section).offsetHeight-22; 函数asideHeightSet(aside,section){var sectionHeight = $(section).offsetHeight-22;

$(aside).style.height = sectionHeight+"px"; $(aside).style.height = sectionHeight +“ px”;

} }

the 22 was to take into account for padding and border of aside. 22是要考虑到边距和边框。

Running the code you provided lets me get the height of the elements with 运行您提供的代码可以让我了解元素的高度

$(section).offsetHeight

style height returns none because probably there are no height css style associated with the element. style height不返回任何值,因为可能没有与元素相关联的height css样式。 Try it and tell me what the results are. 试试看,告诉我结果如何。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM