js get a div's height (width) and assigned to another div

Copyright: https://blog.csdn.net/Yugoup/article/details/84147713

js get height of a div and assign them to another div


When the height of the div is dynamic, and highly adaptive parent div can not, at this time can be used to dynamically obtain js dynamic div height of the child, and assigned to the parent div

With the assumption that the parent div can not change the height of the sub-div change

The first is the HTML page, here I will give an example

<div id="faster">
	<div id="son">
		这个div高度为动态,并且大于父级div的高度
	</div>
</div>

CSS section

<style>
	#faster{
		width: 500px; 
		height: 200px; 
		background: red;
	}
	#son{
		width: 200px; 
		height: 400px; 
		background: deepskyblue;
		float: right;
	}
</style>

Really ugly to the page can not breathe

Ugly ah!  !  !  !  !

js portion, there is provided a native jQ writing and writing
may be used a method offsetHeight

//原生js
<script>
	var fasterheight = document.getElementById('faster');
		var sonheight = document.getElementById('son');
		fasterheight.style.height = sonheight.offsetHeight+'px';
</script>
//jQ写法
<script src="http://code.jquery.com/jquery-1.4.1.js"></script>//使用jquery需要引用官方文件
<script>
		window.onload = function() { 
			var fasterHeight = $('#son').height(); 
   		$("#faster").css({height: fasterHeight+'px'});
  }
</script>

Results are as follows:
Here Insert Picture Description
obtaining dynamic width similar method, using methods offsetWidth

Guess you like

Origin blog.csdn.net/Yugoup/article/details/84147713