在线演示:http://demo.jb51.net/js/2012/jquery_demo/jquery_div_autoheihet.htm
完整代码:
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>jQuery实现左右div自适应高度完全相同 – 软件开发网</title>
<meta name=”Copyright” content=”脚本分享网 //www.jb51.net/” />
<meta name=”description” content=”jQuery实现左右div自适应高度完全相同” />
<meta content=”jQuery实现左右div自适应高度完全相同,JavaScript,分享,JavaScript代码” name=”keywords” />
<style type=”text/css”>
<!–
body{FONT-SIZE: 14px;background-color:#fff}
–>
</style>
<style type=”text/css”>
#left{background:#999999; float:left; width:100px;}
#right{background:#0066FF; color:#fff; width:300px; float:left;}
.clear{clear:both;}
</style>
</head>
<body>
<h3>右边高度高度左边</h3>
<div id=”left”>
<div style=”padding:10px”>
<a href=”//www.jb51.net”>软件开发网</a>
</div>
</div>
<div id=”right”>
<div style=”padding:10px”>
是一个以网站设计相关的资源分享网站,网站只提供最优秀的<a href=”//www.jb51.net/”>JS代码</a>,jQuery插件,网页特效,HTML5代码,矢量图,设计图标,网站模板等,而且这一切都是免费的
</div>
</div>
<script type=”text/javascript” src=”https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js”></script>
<script type=”text/javascript”>
function $(id){
return document.getElementById(id)
}
function getHeight() {
if ($(“left”).offsetHeight>=$(“right”).offsetHeight){
$(“right”).style.height=$(“left”).offsetHeight + “px”;
}
else{
$(“left”).style.height=$(“right”).offsetHeight + “px”;
}
}
window.onload = function() {
getHeight();
}
</script>
<div style=”clear:both”></div>
</body>
</html>
这其中的jquery代码中的获得高度用的clientHeight,介绍一下几种不同的获得方式以及他们的差别。
这四种浏览器分别为IE(Internet Explorer)、NS(Netscape)、Opera、FF(FireFox)。
clientHeight
大家对 clientHeight 都没有什么异议,都认为是内容可视区域的高度,也就是说页面浏览器中可以看到内容的这个区域的高度,一般是最后一个工具条以下到状态栏以上的这个区域,与页面内容无关。










