Sabtu, 23 Agustus 2025

DHTML - Current Style

 

currentStyle Object


Represents the cascaded format and style of the object as specified by global style sheets, inline styles, and HTML attributes.

Remarks

The currentStyle object returns the cascaded styles on an element, but the style object returns only the styles that have been applied inline on an element through the style attribute. Thus, the style values retrieved through the currentStyle object might differ from the style values retrieved through the style object. For example, if the color property is set on a paragraph only through a linked or embedded style sheet, and not inline, then object.currentStyle.color returns the color, whereas object.style.color does not return a value. If, however, the author specifies <P STYLE="color:'red'", the currentStyle and style objects return the value red.

The currentStyle object reflects the order of style precedence in cascading style sheets (CSS). The CSS order of precedence for the presentation of HTML is:

  1. Inline styles
  2. Style sheet rules
  3. Attributes on HTML tags
  4. Intrinsic definition of the HTML tag

Accordingly, the currentStyle object returns the fontWeight value normal on a bold tag if normal is specified in a style sheet.

The currentStyle object returns values that reflect the applied style settings for the page and might not reflect what is rendering at the time a value is retrieved. For example, an object that has "color:red; display:none" returns currentStyle.color as red even though the object is not rendered on the page. The currentStyle object, then, is not affected by the rendering constraints. The third example in the Example section demonstrates this behavior. Disabled style sheets also do not affect currentStyle values.

The returned value is in the same units as those used to set the object. For example, if the color of an object is set inline using STYLE="color:'green'", then object.currentStyle.color returns green and not #00FF00 (the red-green-blue hexadecimal equivalent to green). However, capitalization and redundant white space that appear in the object values set by the author are lost when the currentStyle object returns the object values.

The currentStyle object supports user-defined properties in style rules. See the second example in the Example section.

The currentStyle object is asynchronous. This means a style cannot be set and then immediately queried—instead, the old value is returned. Thus, for a script to obtain the expected behavior of currentStyle with methods such as addImport, the script needs to include a function that calls the method and a function that checks currentStyle. For a script to check the current style while a page is loading, the script must wait until the body element is loaded and the page has rendered, or the value of currentStyle might not reflect what is being displayed.

This object is available in script as of Microsoft® Internet Explorer 5.

PropertyDescription
blockDirectionRetrieves a string value that indicates whether the content in the block element flows from left to right, or from right to left.
clipBottomRetrieves the bottom coordinate of the object clipping region.
onOffBehaviorRetrieves an object indicating whether the specified Microsoft® DirectAnimation® behavior is running.
MethodDescription
getAttributeRetrieves the value of the specified attribute.
getExpressionRetrieves the expression for the given property.
setAttributeSets the value of the specified attribute.
setExpressionSets an expression for the specified object.
Style AttributeStyle PropertyDescription
ACCELERATORacceleratorRetrieves a string that indicates whether the object contains an accelerator key.
background-attachmentbackgroundAttachmentRetrieves how the background image is attached to the object within the document.
background-colorbackgroundColorRetrieves the color behind the content of the object.
background-imagebackgroundImageRetrieves the background image of the object.
background-position-xbackgroundPositionXRetrieves the x-coordinate of the backgroundPosition property.
background-position-ybackgroundPositionYRetrieves the y-coordinate of the backgroundPosition property.
background-repeatbackgroundRepeatRetrieves how the backgroundImage property of the object is tiled.
behaviorbehaviorRetrieves the location of the DHTML Behaviors.
border-bottom-colorborderBottomColorRetrieves the color of the bottom border of the object.
border-bottom-styleborderBottomStyleRetrieves the style of the bottom border of the object.
border-bottom-widthborderBottomWidthRetrieves the width of the bottom border of the object.
border-collapseborderCollapseRetrieves a value that indicates whether the row and cell borders of a table are joined in a single border or detached as in standard HTML.
border-colorborderColorRetrieves the border color of the object.
border-left-colorborderLeftColorRetrieves the color of the left border of the object.
border-left-styleborderLeftStyleRetrieves the style of the left border of the object.
border-left-widthborderLeftWidthRetrieves the width of the left border of the object.
border-right-colorborderRightColorRetrieves the color of the right border of the object.
border-right-styleborderRightStyleRetrieves the style of the right border of the object.
border-right-widthborderRightWidthRetrieves the width of the right border of the object.
border-styleborderStyleRetrieves the style of the left, right, top, and bottom borders of the object.
border-top-colorborderTopColorRetrieves the color of the top border of the object.
border-top-styleborderTopStyleRetrieves the style of the top border of the object.
border-top-widthborderTopWidthRetrieves the width of the top border of the object.
border-widthborderWidthRetrieves the width of the left, right, top, and bottom borders of the object.
bottombottomRetrieves the bottom position of the object in relation to the bottom of the next positioned object in the document hierarchy.
clearclearRetrieves whether the object allows floating objects on its left side, right side, or both, so that the next text displays past the floating objects.
clipLeftRetrieves the left coordinate of the object clipping region.
clipRightRetrieves the right coordinate of the object clipping region.
clipTopRetrieves the top coordinate of the object clipping region.
colorcolorRetrieves the color of the text of the object.
cursorcursorRetrieves the type of cursor to display as the mouse pointer moves over the object.
directiondirectionRetrieves the reading order of the object.
displaydisplayRetrieves whether the object is rendered.
font-familyfontFamilyRetrieves the name of the font used for text in the object.
font-sizefontSizeRetrieves the size of the font used for text in the object.
font-stylefontStyleRetrieves the font style of the object as italicnormal, or oblique.
font-variantfontVariantRetrieves whether the text of the object is in small capital letters.
font-weightfontWeightRetrieves the weight of the font of the object.
hasLayoutRetrieves a value that indicates whether the object has layout.
heightheightRetrieves the height of the object.
ime-modeimeModeRetrieves the state of an Input Method Editor (IME).
layout-flowlayoutFlowRetrieves the direction and flow of the content in the object.
layout-grid-charlayoutGridCharRetrieves the size of the character grid used for rendering the text content of an element.
layout-grid-linelayoutGridLineRetrieves the gridline value used for rendering the text content of an element.
layout-grid-modelayoutGridModeRetrieves whether the text layout grid uses two dimensions.
layout-grid-typelayoutGridTypeRetrieves the type of grid used for rendering the text content of an element.
leftleftRetrieves the position of the object relative to the left edge of the next positioned object in the document hierarchy.
letter-spacingletterSpacingRetrieves the amount of additional space between letters in the object.
line-breaklineBreakRetrieves line-breaking rules for Japanese text.
line-heightlineHeightRetrieves the distance between lines in the object.
list-style-imagelistStyleImageRetrieves which image to use as a list-item marker for the object.
list-style-positionlistStylePositionRetrieves how the list-item marker is drawn relative to the content of the object.
list-style-typelistStyleTypeRetrieves the predefined type of the line-item marker for the object.
marginmarginRetrieves the width of the top, right, bottom, and left margins of the object.
margin-bottommarginBottomRetrieves the height of the bottom margin of the object.
margin-leftmarginLeftRetrieves the width of the left margin of the object.
margin-rightmarginRightRetrieves the width of the right margin of the object.
margin-topmarginTopRetrieves the height of the top margin of the object.
min-heightminHeightRetrieves the minimum height for an element.
overflowoverflowRetrieves a value indicating how to manage the content of the object when the content exceeds the height or width of the object.
overflow-xoverflowXRetrieves how to manage the content of the object when the content exceeds the width of the object.
overflow-yoverflowYRetrieves how to manage the content of the object when the content exceeds the height of the object.
paddingpaddingRetrieves the amount of space to insert between the object and its margin or, if there is a border, between the object and its border.
padding-bottompaddingBottomRetrieves the amount of space to insert between the bottom border of the object and the content.
padding-leftpaddingLeftRetrieves the amount of space to insert between the left border of the object and the content.
padding-rightpaddingRightRetrieves the amount of space to insert between the right border of the object and the content.
padding-toppaddingTopRetrieves the amount of space to insert between the top border of the object and the content.
page-break-afterpageBreakAfterRetrieves a value indicating whether a page break occurs after the object.
page-break-beforepageBreakBeforeRetrieves a string indicating whether a page break occurs before the object.
positionpositionRetrieves the type of positioning used for the object.
rightrightRetrieves the position of the object relative to the right edge of the next positioned object in the document hierarchy.
ruby-alignrubyAlignRetrieves the position of the ruby text specified by the rt object.
ruby-overhangrubyOverhangRetrieves the position of the ruby text specified by the rt object.
ruby-positionrubyPositionRetrieves the position of the ruby text specified by the rt object.
scrollbar-3dlight-colorscrollbar3dLightColorRetrieves the color of the top and left edges of the scroll box and scroll arrows of a scroll bar.
scrollbar-arrow-colorscrollbarArrowColorRetrieves the color of the arrow elements of a scroll arrow.
scrollbar-base-colorscrollbarBaseColorRetrieves the color of the main elements of a scroll bar, which include the scroll box, track, and scroll arrows.
scrollbar-darkshadow-colorscrollbarDarkShadowColorRetrieves the color of the gutter of a scroll bar.
scrollbar-face-colorscrollbarFaceColorRetrieves the color of the scroll box and scroll arrows of a scroll bar.
scrollbar-highlight-colorscrollbarHighlightColorRetrieves the color of the top and left edges of the scroll box and scroll arrows of a scroll bar.
scrollbar-shadow-colorscrollbarShadowColorRetrieves the color of the bottom and right edges of the scroll box and scroll arrows of a scroll bar.
scrollbar-track-colorscrollbarTrackColorRetrieves the color of the track element of a scroll bar.
floatstyleFloatRetrieves on which side of the object the text will flow.
table-layouttableLayoutRetrieves a string that indicates whether the table layout is fixed.
text-aligntextAlignRetrieves whether the text in the object is left-aligned, right-aligned, centered, or justified.
text-align-lasttextAlignLastRetrieves how to align the last line or only line of text in the object.
text-autospacetextAutospaceRetrieves the autospacing and narrow space width adjustment of text.
text-decorationtextDecorationRetrieves whether the text in the object has blink, line-through, overline, or underline decorations.
text-indenttextIndentRetrieves the indentation of the first line of text in the object.
text-justifytextJustifyRetrieves the type of alignment used to justify text in the object.
text-kashida-spacetextKashidaSpaceRetrieves the ratio of kashida expansion to white space expansion when justifying lines of text in the object.
text-overflowtextOverflowRetrieves a value that indicates whether to render ellipses(...) to indicate text overflow.
text-transformtextTransformRetrieves the rendering of the text in the object.
text-underline-positiontextUnderlinePositionRetrieves the position of the underline decoration that is set through the textDecoration property of the object.
toptopRetrieves the position of the object relative to the top of the next positioned object in the document hierarchy.
unicode-bidiunicodeBidiRetrieves the level of embedding with respect to the bidirectional algorithm.
vertical-alignverticalAlignRetrieves the vertical alignment of the object.
visibilityvisibilityRetrieves whether the content of the object is displayed.
white-spacewhiteSpaceRetrieves a value that indicates whether lines are automatically broken inside the object.
widthwidthRetrieves the width of the object.
word-breakwordBreakRetrieves line-breaking behavior within words, particularly where multiple languages appear in the object.
word-spacingwordSpacingRetrieves the amount of additional space between words in the object.
word-wrapwordWrapRetrieves whether to break words when the content exceeds the boundaries of its container.
writing-modewritingModeRetrieves the direction and flow of the content in the object.
z-indexzIndexRetrieves the stacking order of positioned objects.
zoomzoomRetrieves the magnification scale of the object.

Examples

This example uses the currentStyle object to set the text color to brown. If you click a colored area and the background color is the same as the text color, the checkColor function changes the background color, so the text can be read. Otherwise, the function takes no action.

This example works only if the body and text colors are set using either color names or red-green-blue hexadecimal values, but not a mix of the two.

<SCRIPT>
function checkColor(oObj)
{
  if (oObj.currentStyle.backgroundColor == 'brown')
	{
        oObj.style.backgroundColor = 'white';
	}
  else
	:
}
</SCRIPT>
</HEAD>
:
<P STYLE="background-color: 'brown'"
    onclick="checkColor(this)">
This feature requires Microsoft® Internet Explorer 5 or later. Click the icon below to install the latest version. Then reload this page to view the sample.

This example uses the currentStyle object to retrieve values of the user-defined property created in the style rule. The alert returns the value myvalue.

<STYLE>
    P { myproperty:myvalue }
</STYLE>
<BODY>
<P ID=oPrgrph>
:
<SCRIPT>
alert(oPrgrph.currentStyle.myproperty)
</SCRIPT>
This feature requires Microsoft® Internet Explorer 5 or later. Click the icon below to install the latest version. Then reload this page to view the sample.

This example shows that the td object width returned by the currentStyle object is its cascaded width value rather than the width rendered on the screen.

<BODY ID=oBdy>
:
<TABLE BORDER>
<TR><TD WIDTH=1100 ID=oTblD>text</TD></TR>
</TABLE>
:
<SCRIPT>
alert("The TD object currentStyle.width is " + oTblD.currentStyle.width +
    ".\nThe width of the window is " + oBdy.clientWidth +
    "px.\nThe width of the screen is " + screen.width + "px." )
</SCRIPT>
This feature requires Microsoft® Internet Explorer 5 or later. Click the icon below to install the latest version. Then reload this page to view the sample.

Standards Information

There is no public standard that applies to this object.

Applies To

AACRONYMADDRESSAPPLETBBDOBIGBLOCKQUOTEBODYBRBUTTONCAPTIONCENTERCITECODECOLCOLGROUPCUSTOMDDDELDFNDIRDIVDLDTEMEMBEDFIELDSETFONTFORMHnHTMLIIMGINPUT type=buttonINPUT type=checkboxINPUT type=fileINPUT type=imageINPUT type=passwordINPUT type=radioINPUT type=resetINPUT type=submitINPUT type=textINSISINDEXKBDLABELLEGENDLILISTINGMARQUEEMENUNOBROBJECTOLOPTIONPPLAINTEXTPREQSSAMPSELECTSMALLSPANSTRIKESTRONGSUBSUPTABLETBODYTDTEXTAREATFOOTTHTHEADTRTTUULVARWBRXMP

See Also

style

Tidak ada komentar: