首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

如果获得一个div的宽高,该怎么处理

2012-04-07 
如果获得一个div的宽高divid test 你知道我的长宽么 /divscriptvartestdocument.getElementByI

如果获得一个div的宽高
<div   id= "test "> 你知道我的长宽么 </div>

<script>
var   test=document.getElementById( "test ");
document.write( "长: "+test.style.pixelWidth);
document.write( "长: "+test.width);
</script>


这个div没有进行任何width,height的定义,里面写了些文字
想知道这个div的宽高,如何实现?

[解决办法]
<div id= "test " style= "background-color: yellow "> 你知道我的长宽么 </div>

<script>
var test=document.getElementById( "test ");
document.write( "宽: "+test.offsetWidth);
document.write( "高: "+test.offsetHeight);
</script>

妥了,哈
[解决办法]
如果设置高度和宽度
可以用currentStyle.height和currentStyle.width
如果没有设置,使用offsetHeight
<div id= "div1 ">
asdasd
</div>
<script language=javascript>
alert(document.getElementById( "div1 ").offsetHeight)
alert(document.getElementById( "div1 ").offsetWidth)
</script>

热点排行