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");