1. js 获取div所填充内容的实际高度
<html>
<head>
<title>js获取div所填充内容的实际高度</title>
</head>
<body>
<divid="div1">
网络知道是一个基于搜索的互动式知识问答分享平台,于2005年6月21日发布,并于2005年11月8日转为正式版。网络知道一直探索国际化发展,于2012年3月31日发布网络知道台湾版。
</div>
<buttontype="submit"onclick="test()">点击获取</button>
<scripttype="text/javascript">
functiontest(){
varoDiv=document.getElementById('div1');
alert(oDiv.offsetHeight);
}
</script>
</body>
</html>
下面结合各上图介绍一下各个属性的作用:
一.offsetTop属性:
此属性可以获取元素的上外缘距离最近采用定位父元素内壁的距离,如果父元素中没有采用定位的,则是获取上外边缘距离文档内壁的距离。所谓的定位就是position属性值为relative、absolute或者fixed。
返回值是一个整数,单位是像素。
此属性是只读的。
二.offsetLeft属性:
此属性和offsetTop的原理是一样的,只不过方位不同,这里就不多介绍了。
三.offsetWidth属性:
此属性可以获取元素的宽度,宽度值包括:元素内容+内边距+边框。不包括外边距和滚动条部分。
返回值是一个整数,单位是像素。
此属性是只读的。
四.offsetHeight属性:
此属性可以获取元素的高度,宽度值包括:元素内容+内边距+边框。不包括外边距和滚动条部分。
返回值是一个整数,单位是像素。
此属性是只读的。
五.clientWidth属性:
此属性可以返回一个元素的宽度值,值是:元素的内容+内边距。不包括边框、外边距和滚动条部分。
返回值是一个整数,单位是像素。
此属性是只读的。
六.clientHeight属性:
此属性可以返回一个元素的高度值,值是:元素的内容+内边距。不包括边框、外边距和滚动条部分。
返回值是一个整数,单位是像素。
此属性是只读的。
七.scrollLeft属性:
此属性可以获取或者设置对象的最左边到对象在当前窗口显示的范围内的左边的距离,也就是元素被滚动条向左拉动的距离。
返回值是一个整数,单位是像素。
此属性是可读写的。
八.scrollTop属性:
此属性可以获取或者设置对象的最顶部到对象在当前窗口显示的范围内的顶边的距离,也就是元素滚动条被向下拉动的距离。
返回值是一个整数,单位是像素。
2. javascript如何获取div的class中设置的宽高度
||<div id="d1" class="div1">div1</div>
var d = document.getElementId("d1");
var w = d.style["width"] ||抄 d.width;
var h = d.style["height"] || d.height;
我就没有袭听说过用那个就获取不到宽和高了
3. js如何计算并赋予DIV高度
js里面提供来了很多的方法,源可以计算DIV的高度,以及也可以给DIV直接复制。
如下是代码的实现:
<body>
<divstyle="width:100px;height:100px;background:#ccc;"id="div"></div>
</body>
<script>
varoDiv=document.getElementById('div');
alert(oDiv.offsetHeight);//这个就是获取DIV的高度
oDiv.style.height=oDiv.offsetHeight+100+'px';//这个就是直接给DIV的高度赋值。
</script>
4. 如何用js获取div的高度
functionAdjustColumnsHeight(){
varmainCol=window.document.getElementById('MainColumn');
varleftCol=window.document.getElementById('MainLeft');
varrightCol=window.document.getElementById('MainRight');
varhMainCol=mainCol.offsetHeight;
varhLeftCol=leftCol.offsetHeight;
varhRightCol=rightCol.offsetHeight;
varmaxHeight=Math.max(hMainCol,Math.max(hLeftCol,hRightCol));
mainCol.style.height=maxHeight+'px';
leftCol.style.height=maxHeight+'px';
rightCol.style.height=maxHeight+'px';
}
上面的代码是取得3个div的高度,并判断高度最高的div高度值,让其余两个比较矮的div高度等于最高这个,自己琢磨下,很简单。
5. js如何给div增加高度
function AdjustColumnsHeight() {
var mainCol = window.document.getElementById('MainColumn');
var leftCol = window.document.getElementById('MainLeft');
var rightCol = window.document.getElementById('MainRight');
var hMainCol = mainCol .offsetHeight;
var hLeftCol = leftCol.offsetHeight;
var hRightCol = rightCol.offsetHeight;
var maxHeight = Math.max( hMainCol , Math.max(hLeftCol, hRightCol));
mainCol.style.height = maxHeight + 'px';
leftCol.style.height = maxHeight + 'px';
rightCol.style.height = maxHeight + 'px';
}
上面的代码是取得3个div的高度,并判断高度最高的div高度值,让其余两个比较矮的div高度等于最高这个,自己琢磨下,很简单。
6. 如何用 js 取得浏览器的高度并赋值给div
js 取得浏览器的高度并赋值给div的步骤如下:
1. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><head><title>
2.请调整浏览器窗口大小</h2><hr/><formaction="#"method="get"name="form1"id="form1"><!--
3.显示浏览器窗口的实际尺寸-->浏览器窗口的实际高 度:<inputtype="text"name="availHeight"size="4"/><br/>
4.浏览器窗口的实际宽度:<inputtype="text"name="availWidth"size="4"/><br/></form><scripttype="text/javascript"><!--varwinWidth=0;varwinHeight=0;functionfindDimensions()
5.//函数:获取尺寸{//获取窗口宽度if(window.innerWidth)winWidth=window.innerWidth;elseif((document.body)&&(document.body.clientWidth))winWidth=document.body.clientWidth;//获取窗口高度document.form1.availHeight.value=winHeight;document.form1.availWidth.value=winWidth;}findDimensions();//调用函数,获取数值window.onresize=findDimensions;//--></script></body></html>
7. js获取div的style里的height问题
楼主你这抄用的是袭外部样式,非行内样式所以不能这样
function getCss(obj, type) {
if (obj.currentStyle) {
retrun obj.currentStyle[type]
}
else{
return getComputedStyle(obj, flase)[type];
}
}
alert(getCss(el, 'height'));
8. 如何用 js 取得浏览器的高度并赋值给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.w3.org/1999/xhtml"><head>
<title>请调整浏览器窗口</title> <meta http-equiv="content-type" content="text/html; charset=gb2312">
</meta></head>
<body>
<h2 align="center">请调整浏览器窗口大小</h2><hr />
<form action="#" method="get" name="form1" id="form1">
<!--显示浏览器窗口的实际尺寸-->
浏览器窗口 的 实际高度: <input type="text" name="availHeight" size="4"/><br />
浏览器窗口 的 实际宽度: <input type="text" name="availWidth" size="4"/><br />
</form>
<script type="text/javascript">
<!--
var winWidth = 0;
var winHeight = 0;
function findDimensions() //函数:获取尺寸
{
//获取窗口宽度
if (window.innerWidth)
winWidth = window.innerWidth;
else if ((document.body) && (document.body.clientWidth))
winWidth = document.body.clientWidth;
//获取窗口高度
if (window.innerHeight)
winHeight = window.innerHeight;
else if ((document.body) && (document.body.clientHeight))
winHeight = document.body.clientHeight;
//通过深入Document内部对body进行检测,获取窗口大小
if (document.documentElement && document.documentElement.clientHeight && document.documentElement.clientWidth)
{
winHeight = document.documentElement.clientHeight;
winWidth = document.documentElement.clientWidth;
}
//结果输出至两个文本框
document.form1.availHeight.value= winHeight;
document.form1.availWidth.value= winWidth;
}
findDimensions();
//调用函数,获取数值
window.onresize=findDimensions;
//-->
</script>
</body>
</html>
9. javascript动态获得div的高度并输出的问题,谢谢!
alert(document.getElementById("div1").currentStyle.height);
以上适用于IE浏览器,如果是标准浏览器,要这样:
oDiv=document.getElementById("div1");
alert(document.defaultView.getComputedStyle(oDiv,null).height);
注意内,以上两种方式均是只读的容,要写入的话仍然用:....style.height="...."
原因在于CSS有内嵌、内联、外联等方式,Javascript对它们的处理方式不同的。你的方法只适用于内嵌样式表,即 <div style="height:50px"></div>
10. js判断div高度。
超出固定高度显示滚动条,只要设置属性中的overflowY="scroll";
document.getElementById('scroll').style.overflowY="scroll";
获取div的高度,如果获取不到,可用下面的方法,一下方法是获取div的编辑完成后的高度:
/**
*obj:需要获取属性的html对象
*prop:需要获取的obj对象的属性
*/
functiongetCurrentStyle(obj,prop){
if(obj.currentStyle){
returnobj.currentStyle[prop];
}
elseif(window.getComputedStyle){
propprop=prop.replace(/([A-Z])/g,"-$1");
propprop=prop.toLowerCase();
returndocument.defaultView.getComputedStyle(obj,null)[prop];
}
returnnull;
}
vardiv=document.getElementsByTagName("div")[0];
varheight=getCurrentStyle(div,"height");