Retrieves the width of the object relative to the layout or coordinate parent, as specified by the offsetparent property. dhtml语法 html | n/a |
---|
scripting | [ iwidth = ] object.offsetwidth |
---|
dhtml可能的值 iWidth | Integer that
receives the width, in pixels. |
The property is read-only.
The property has no default value.
Remarks You can determine the location, width, and height of an object by using a combination of the offsetleft, offsettop, offsetheight, and offsetwidth properties. these numeric properties specify the physical coordinates and dimensions of the object relative to the object's offset parent. for more information about how to access the dimension and location of objects on the page through the dynamic html(动态html,即dhtml)燿ocument object model (dom), see measuring element dimension and location. to comply with the cascading style sheets, level 1 (css1) box model, microsoft® internet explorer 6 and later calculate the height of objects differently when you use the !doctype declaration in your document to switch on standards-compliant mode. this difference may affect the value of the offsetwidth propety. when standards-compliant mode is switched on, the width property specifies the distance between the left and right edges of the bounding box that surrounds the object's content. when standards-compliant mode is not switched on, and with earlier versions of internet explorer, the width property also includes the border and padding belts that surround the object's bounding box. for more information, see css enhancements in internet explorer 6.
Examples
This example adjusts the size of a clock's readout to fit the current width and height of the document. <HTML>
<HEAD><TITLE>A Simple Clock</TITLE>
<SCRIPT LANGUAGE="JScript">
function startClock()
{
window.setInterval("Clock_Tick()", 1000);
Clock_Tick();
}
var ratio = 4;
function Clock_Tick()
{
var s = Date();
var t = s.substring(11,19);
var doc_height = document.body.offsetHeight;
var doc_width = document.body.offsetWidth;
if ((doc_height*ratio)>doc_width)
doc_height = doc_width / ratio;
document.all.MyTime.innerText = t;
document.all.MyTime.style.fontSize = doc_height;
}
</SCRIPT>
<BODY onload="startClock()">
<P ID="MyTime"> </P>
</BODY>
</HTML>
This example uses the offsetWidth property and the clientwidth property to show the different ways of measuring the object size. <div id=odiv style="overflow:scroll; width:200; height:100"> . . . </DIV>
<BUTTON onclick="alert(oDiv.clientWidth)">client width</BUTTON>
<BUTTON onclick="alert(oDiv.offsetWidth)">offset width</BUTTON> This feature requires Microsoft® Internet Explorer 4.0 or later. Click the following icon to install the latest version. Then reload this page to view the sample.
是否符合公共标准
There is no public standard that applies to this property.
Applies To |
A,
ACRONYM,
ADDRESS,
APPLET,
AREA,
B,
BDO,
BIG,
BLOCKQUOTE,
BODY,
BR,
BUTTON,
CAPTION,
CENTER,
CITE,
CODE,
COL,
COLGROUP,
CUSTOM,
DD,
DEL,
DFN,
DIR,
DIV,
DL,
DT,
EM,
EMBED,
FIELDSET,
FONT,
FORM,
FRAME,
hn,
HR,
I,
IFRAME,
IMG,
INPUT type=button,
INPUT type=checkbox,
INPUT type=file,
INPUT type=image,
INPUT type=password,
INPUT type=radio,
INPUT type=reset,
INPUT type=submit,
INPUT type=text,
INS,
KBD,
LABEL,
LEGEND,
LI,
LISTING,
MAP,
MARQUEE,
MENU,
NOBR,
OBJECT,
OL,
OPTION,
P,
PLAINTEXT,
PRE,
Q,
RT,
RUBY,
S,
SAMP,
SELECT,
SMALL,
SPAN,
STRIKE,
STRONG,
SUB,
SUP,
TABLE,
TBODY,
TD,
TEXTAREA,
TFOOT,
TH,
THEAD,
TR,
TT,
U,
UL,
VAR,
XMP | |
|