clientWidth、offsetWidth、clientHeight、offsetHeight的测试比较

转摘

IE6.0、FF1.06+:
clientWidth = width + padding
clientHeight = height + padding
offsetWidth = width + padding + border
offsetHeight = height + padding + border
IE5.0/5.5:
clientWidth = width - border
clientHeight = height - border
offsetWidth = width
offsetHeight = height
(需要提一下:CSS中的margin属性,与clientWidth、offsetWidth、clientHeight、offsetHeight均无关)

具体效果请参见下面的代码:

js 代码
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[url=http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd[/url]">   
  2. <html xmlns="[url=http://www.w3.org/1999/xhtml]http://www.w3.org/1999/xhtml[/url]" lang="gb2312">   
  3. <head>   
  4. <head>   
  5. <title> 代码实例:关于clientWidth、offsetWidth、clientHeight、offsetHeight的测试比较 </title>   
  6. <meta http-equiv="content-type" content="text/html; charset=gb2312" />   
  7. <meta name="author" content="枫岩,CnLei.y.l@gmail.com">   
  8. <meta name="copyright" content="[url=http://www.cnlei.com]http://www.cnlei.com[/url]" />   
  9. <meta name="description" content="关于clientWidth、offsetWidth、clientHeight、offsetHeight的测试比较" />   
  10. <style type="text/css" media="all">   
  11. body {font-size:14px;}   
  12. a,a:visited {color:#00f;}  
  13. #Div_CnLei {   
  14. width:300px;   
  15. height:200px;   
  16. padding:10px;   
  17. border:10px solid #ccc;   
  18. background:#eee;   
  19. font-size:12px;   
  20. }  
  21. #Div_CnLei p {margin:0;padding:10px;background:#fff;}   
  22. </style>   
  23. <script type="text/javascript">   
  24. function Obj(s){   
  25. return document.getElementById(s)?document.getElementById(s):s;   
  26. }   
  27. function GetClientWidth(o){   
  28. return Obj(o).clientWidth;   
  29. }   
  30. function GetClientHeight(o){   
  31. return Obj(o).clientHeight;   
  32. }   
  33. function GetOffsetWidth(o){   
  34. return Obj(o).offsetWidth;   
  35. }   
  36. function GetOffsetHeight(o){   
  37. return Obj(o).offsetHeight;   
  38. }   
  39. </script>   
  40. </head>   
  41. <body>   
  42. <p>点击下面的链接:</p>   
  43. <div id="Div_CnLei">   
  44. <p><a href="javascript:alert(GetClientWidth('Div_CnLei'));">GetClientWidth();</a>  <a href="javascript:alert(GetClientHeight('Div_CnLei'));">GetClientHeight();</a></p>   
  45. <p><a href="javascript:alert(GetOffsetWidth('Div_CnLei'));">GetOffsetWidth();</a>  <a href="javascript:alert(GetOffsetHeight('Div_CnLei'));">GetOffsetHeight();</a></p>   
  46. </div>   
  47. <div id="Description">   
  48. <p><strong>IE6.0、FF1.06+:</strong><br />   
  49. clientWidth = width + padding = 300+10×2 = 320<br />   
  50. clientHeight = height + padding = 200+10×2 = 220<br />   
  51. offsetWidth = width + padding + border = 300+10×2+10×2= 340<br />   
  52. offsetHeight = height + padding + border = 200+10×2+10×2 = 240</p>   
  53. <p><strong>IE5.0/5.5:</strong><br />   
  54. clientWidth = width - border = 300-10×2 = 280<br />   
  55. clientHeight = height - border = 200-10×2 = 180<br />   
  56. offsetWidth = width = 300<br />   
  57. offsetHeight = height = 200</p>   
  58. </div>   
  59. </body>   
  60. </html>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值