面提供三款利用js 獲取div寬度代碼哦,第一款簡單實用,第二款獲取了div寬度與高度的js實例,第三個實例是一款s獲取指定div裡面所有圖片高度與寬度代碼。
<input type="button" onclick="calculate()" value="test">
<script>
function calculate(){
var divs = document.getelementbyid("showtemptext");
divs.innerhtml="wuao ahshahsah <font size=6>jzread.com</font>";
alert(divs.scrollwidth + "__" + divs.scrollheight);
}
</script>
<html>
<head>
<title>test page</title>
</head>
<body>
<table style="width:100%;" >
<tr>
<td>
<div id="div1" style="background:green;" >div1</div>
<td>
<td style="width:50%;" >
<div id="div2" style="background-color:red;" >div2 www.jzread.com</div>
<td>
</table>
<input type="button" onclick="show()" value="show" />
<script>
function show()
{
var o = document. getelementbyid("div1");
var w = o.clientwidth|| o.offsetwidth;
alert("div1 s width is "+w);
}
</script>
</body>
</html>
下面是一款利用js獲取指定div裡面所有圖片高度與寬度js怎麼獲取 這個div內圖片的寬度和
<div style="width:100000px;" id="pal">
<img src="/www.jzread.com/images/2009021121575533977. jpg" width="486" height="348" />
<img src="images/2009021121575533977.jpg" />
<img src="images/2009021121575533977.jpg" />
<img src="images/2009021121575533977.jpg" />
<img src="images/2009021121575533977.jpg" />
<img src="images/2009021121575533977.jpg" />
<img src="images/2009021121575533977.jpg" />
<img src="images/2009021121575533977.jpg" / >
</div>
<script>
var obj=document.getelementbyid("pal");
var imgs=obj.getelementsbytagname("img");
var sum=0;
for(var i=0;i<imgs.length;i++)
sum+=imgs[i].width;
alert(sum);
</script>