Dom:关于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均无关)

具体效果请参见下面的代码:
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html  xmlns ="http://www.w3.org/1999/xhtml" lang ="gb2312" >
< head >
< head >
< title > 代码实例:关于clientWidth、offsetWidth、clientHeight、offsetHeight的测试比较 </ title >
< meta  http-equiv ="content-type"  content ="text/html;charset=gb2312"   />
< meta  name ="author"  content ="枫岩,CnLei.y.l@gmail.com" >
< meta  name ="copyright"  content ="http://www.cnlei.com" />
< meta  name ="description" content ="关于clientWidth、offsetWidth、clientHeight、offsetHeight的测试比较" />
< style  type ="text/css"  media ="all" >
body 
{ font-size : 14px ; }
a,a:visited 
{ color : #00f ; }
#Div_CnLei 
{
width
: 300px ;
height
: 200px ;
padding
: 10px ;
border
: 10px solid #ccc ;
background
: #eee ;
font-size
: 12px ;
}
#Div_CnLei p 
{ margin : 0 ; padding : 10px ; background : #fff ; }
</ style >

< script  type ="text/javascript" >
function  Obj(s){
 returndocument.getElementById(s)
? document.getElementById(s):s;
}
function  GetClientWidth(o){
 
return  Obj(o).clientWidth;
}
function  GetClientHeight(o){
 
return  Obj(o).clientHeight;
}
function  GetOffsetWidth(o){
 
return  Obj(o).offsetWidth;
}
function  GetOffsetHeight(o){
 
return  Obj(o).offsetHeight;
}
</ script >
</ head >
< body >
< p > 点击下面的链接: </ p >
< div  id ="Div_CnLei" >
< p >< ahref ="javascript:alert(GetClientWidth('Div_CnLei'));" > GetClientWidth(); </ a >    < ahref ="javascript:alert(GetClientHeight('Div_CnLei'));" > GetClientHeight(); </ a ></ p >
< p >< ahref ="javascript:alert(GetOffsetWidth('Div_CnLei'));" > GetOffsetWidth(); </ a >    < ahref ="javascript:alert(GetOffsetHeight('Div_CnLei'));" > GetOffsetHeight(); </ a ></ p >
</ div >
< div  id ="Description" >
< p >< strong > IE6.0、FF1.06+: </ strong >< br />
clientWidth = width + padding = 300+10×2 = 320
< br  />
clientHeight = height + padding = 200+10×2 = 220
< br  />
offsetWidth = width + padding + border = 300+10×2+10×2= 340
< br />
offsetHeight = height + padding + border = 200+10×2+10×2 =240
</ p >
< p >< strong > IE5.0/5.5: </ strong >< br  />
clientWidth = width - border = 300-10×2 = 280
< br  />
clientHeight = height - border = 200-10×2 = 180
< br  />
offsetWidth = width = 300
< br  />
offsetHeight = height = 200
</ p >
</ div >
</ body >
</ html >



本文转自Sam Lin博客园博客,原文链接:http://www.cnblogs.com/samlin/archive/2008/04/05/1138308.html,如需转载请自行联系原作者

目录
相关文章
|
XML JavaScript 测试技术
Web自动化测试框架(基础篇)--HTML页面元素和DOM对象
本文为Web自动化测试入门指南,介绍了HTML页面元素和DOM对象的基础知识,以及如何使用Python中的Selenium WebDriver进行元素定位、操作和等待机制,旨在帮助初学者理解Web自动化测试中的关键概念和操作技巧。
216 1
|
前端开发 JavaScript 测试技术
全网最细:Jest+Enzyme测试React组件(包含交互、DOM、样式测试)
Jest是目前前端工程化下单元测试火热的技术栈,而Enzyme的支持提供了Jest测试React业务、组件的能力,下面来介绍一下React组件测试的一些实际场景。
277 1
|
XML JavaScript 前端开发
|
JavaScript 测试技术 前端开发
|
XML Java 测试技术
【dom4j xml】使用dom4j处理XML文件--测试过程遇到的问题
首先 关于dom4j的API,有如下: 当然  其中的实体引用有以下: 测试使用环境: 使用Maven搭建web环境,pom.xml文件配置如下: 1 2 4.0.0 3 com.
1275 0
|
JavaScript
HTML DOM 节点树
HTML DOM 节点是指在 HTML 文档对象模型中,文档中的所有内容都被视为节点。整个文档是一个文档节点,每个 HTML 元素是元素节点,元素内的文本是文本节点,属性是属性节点,注释是注释节点。DOM 将文档表示为节点树,节点之间有父子和同胞关系。

热门文章

最新文章