共用方式為


getAttribute method

Retrieves the value of the specified attribute.

 

Syntax

 retVal = object.getAttribute(strAttributeName, lFlags);

Parameters

strAttributeName [in]

Type: BSTR

BSTR that specifies the name of the attribute.

lFlags [in, optional]

Type: long

long that specifies one or more of the following flags:

0

Default. Performs a property search that is not case-sensitive, and returns an interpolated value if the property is found.

1

Performs a case-sensitive property search. To find a match, the uppercase and lowercase letters in strAttributeName must exactly match those in the attribute name.

2

Returns attribute value as a BSTR. This flag does not work for event properties.

4

Returns attribute value as a fully expanded URL. Only works for URL attributes.

Standards information

Remarks

Windows Internet Explorer 8 and later. IE8 Standards mode enables several enhancements to the IHTMLElement6::setAttribute, IHTMLElement6::getAttribute, and IHTMLElement6::removeAttribute methods that are not available when pages are displayed in earlier document modes.

  • The strAttributeName parameter requires the name of the desired content attribute and not the Document Object Model (DOM) attribute. For example, in IE8 mode, this method no longer requires strAttributeName to be "className" when setting, getting, or removing a IHTMLElement::className attribute. Earlier versions of Windows Internet Explorer and Internet Explorer 8 in compatibility mode still require strAttributeName to specify the corresponding DOM property name.

  • The strAttributeName parameter is not case sensitive. As a result, the lFlags parameter is no longer supported and should not be used.

  • The methods support event handlers. For example, the following code example defines an event handler to call a function called SomeFunction when the body of the page is loaded.

    document.body.setAttribute('onload', 'SomeFunction()');
    

Internet Explorer 8 or later. In IE8 mode, the IHTMLDOMAttribute2::value property is correctly reported as a canonical attribute name. For example, <input type="text" readonly> and <input type="text" readonly="readonly"> would both set the input text field to read-only. In IE8 mode, the value is evaluated as a canonical value, "readonly". In IE7 and earlier modes, it is evaluated as a Boolean value, VARIANT_TRUE. For more information, see Attribute Differences in Internet Explorer 8

If two or more attributes have the same name (differing only in uppercase and lowercase letters) and lFlags is 0, the IHTMLElement6::getAttribute method retrieves values only for the last attribute created with this name, and ignores all other attributes with the same name.

Internet Explorer 8 and later. In IE7 Standards mode and IE5 (Quirks) mode, attributes that represent URLs and file paths return the same value whether you retrieve them as a DOM property or as a content attribute (using IHTMLElement6::getAttribute). Some attributes return relative URLs, whereas others always return a fully qualified URL. In IE8 mode, DOM attributes always return fully qualified URLs for URL attributes; to retrieve the attribute value as specified in the content, use IHTMLElement6::getAttribute. The following attributes return URLs: IHTMLFormElement4::action, IHTMLBodyElement::background, IHTMLObjectElement::BaseHref, IHTMLBlockElement3::cite, IHTMLObjectElement4::codeBase, IHTMLObjectElement4::data, IHTMLImgElement3::dynsrc, IHTMLLinkElement4::href, IHTMLFrameElement3::longDesc, IHTMLImgElement3::lowsrc, IHTMLEmbedElement2::pluginspage, IHTMLHeadElement2::profile, IHTMLInputElement3::src, url, and IHTMLImgElement3::vrml.

The strAttributeName parameter requires the name of the desired content attribute and not the DOM attribute. For example, this method does not require strAttributeName to be "className" when setting, getting, or removing a IHTMLElement::className attribute.

The strAttributeName parameter is not case sensitive. As a result, the lFlags parameter is no longer supported and should not be used.

The methods support event handlers. For example, the following code example defines an event handler to call a function called SomeFunction when the body of the page is loaded.

document.body.setAttribute('onload', 'SomeFunction()');

If two or more attributes have the same name (differing only in uppercase and lowercase letters) and lFlags is 0, the IHTMLElement6::getAttribute method retrieves values only for the last attribute created with this name, and ignores all other attributes with the same name.

See also

IHTMLElement6::removeAttribute

IHTMLElement6::setAttribute