
用js获取呗,你获取不到应该是你的div没设置宽高,而你用documentgetElementByID("ObjID")styleheight|width来获取的吧,这样只能获取设定了宽高的对象 没有设定的需要用offsetWidth|offsetHeight来获取,比如:documentgetElementByID("ObjID")offsetWidth|offsetHeight
<div id="cao" style="float:left">asdasd<br />asdsadasd</div>
<script>alert(documentgetElementById("cao")offsetWidth)</script>
如果这样获取不到,老子把电脑吃了,你TM问题又不叙述清楚,最TM烦你这种白痴问问题了,天知道你用了什么变态方法
js里面提供了很多的方法,可以计算DIV的高度,以及也可以给DIV直接复制。
如下是代码的实现:
<body><div style="width:100px; height:100px; background:#ccc;" id="div"></div>
</body>
<script>
var oDiv = documentgetElementById('div');
alert(oDivoffsetHeight); //这个就是获取DIV的高度
oDivstyleheight = oDivoffsetHeight + 100 + 'px'; //这个就是直接给DIV的高度赋值。
</script>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>height</title>
<style type="text/css">
div{ height:auto; width:200px; border:1px solid #f00;}
</style>
<!--div就是找到class="div"的信息,用height()方法就可以得到对象的高度的,div信息加载完成,自然就会自己的高度,这时候读取就可以了(深圳网站建设=">
给div设置一个id或者class:
html部分:
<div id="mydiv"></div>
js部分:
<script>
//获取mydiv
var mydiv=documentgetElementById("mydiv");
//获取#mydiv的高度
var divheight=mydivstyleheight;
//设置mydiv行高
mydivstylelineHeight=divheight;
</script>
getElementsByClassName
就是通过class名称来做选择器,
比如你选择class="a" 那就可以通过
documentgetElementsByClassName("a")这样的试为选择就可以了。
以上就是关于如何获取 自动适应DIV 的高宽全部的内容,包括:如何获取 自动适应DIV 的高宽、js如何计算并赋予DIV高度、请问一下:css中将div的height设置为auto,在js中怎么取出这个height的实际值等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)