js如何获取和设置div层的高度和宽度?
js 获取div高度并重新设置div高度与宽度
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.111cn.net/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>js 获取div高度并重新设置div高度与宽度</title>
<script language="javascript教程">
var $=function(id) {
return document.getElementById(id);
}
function clickDiv()
{
$('testid').offsetWidth; // 20
$('testid').offsetHeight; //200
document.all.testid.style.width="1000px"; //js设置div宽度
document.all.testid.style.height="500px"; //设置div高度民主
//现在来重新设置一下testid的高度与宽度
//$('testid').offsetWidth = 300;
//$('testid').offsetHeight =300;
$('testid').style.className = 'idtest';
}
</script>
方法二
<script type="text/javascript">
alert(document.getElementById("testid").scrollHeight+'px');
</script>
方法三
<script type="text/javascript">
alert(document.getElementById("testid").clientHeight+'px');
</script>
<style type="text/css教程">
#testid{
height:50px;
width:200px;
background:#3300CC;
}
.idtest{
height:80px;
width:150px;
background:#FFFFFF;
}
</style>
</head>
<body>
<div id="testid">
</div>
<input type="button" value="点击我" onclick="clickDiv();" />
</body>
</html>
本文地址:http://www.45fan.com/dnjc/13388.html