EN
Ce contenu n’est pas disponible dans votre langue. Voici la version anglaise.

setAttribute method

Sets the value of the specified attribute.

Document Object Model (DOM) Level 2 HTML Specification, Section 1.6.5

 

Syntax

object.setAttribute(strAttributeName, AttributeValue, lFlags)

Parameters

strAttributeName [in]

Type: String

String that specifies the name of the attribute.

AttributeValue [in]

Type: Variant

Variant that specifies the string, number, or Boolean to assign to the attribute.

lFlags [in, optional]

Type: Integer

Integer that specifies whether to use a case-sensitive search to locate the attribute. Can be one of the following values:

1

The case of strAttributeName is respected.

0

Match strAttributeName regardless of case.

Return value

Type: HRESULT

If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.

Standards information

Remarks

If the specified attribute is not already present, the setAttribute method adds the attribute to the object and sets the value.

If your pages are displayed in a Windows Store app using JavaScript or a Windows Store app using JavaScript, be careful when spelling attribute names. If you set lFlags to 1 and the strAttributeName parameter does not have the same uppercase and lowercase letters as the attribute, a new attribute is created for the object. If two or more attributes have the same name, differing only in case, and lFlags is set to 0, this method assigns values only to the first attribute created with this name. All other attributes of the same name are ignored.

The strAttributeName parameter requires the name of the desired content attribute.

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

The AttributeValue parameter only supports string values. Non-string values are not automatically converted to string values. For best results, formally convert values to strings before using them as parameter values. For example, do not attempt to pass an object directly to the AttributeValue parameter; call the object's toString method instead.

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 document is loaded.


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

removeAttribute always returns S_OK as an HRESULT value. Check the pfSuccess parameter to determine if the attribute is successfully removed.

Examples

Click the following Show Me button to see example usage of the setAttribute method.

See also

a
address
area
b
base
bgSound
blockQuote
body
br
button
caption
cite
code
col
colGroup
comment
currentStyle
custom
dd
dfn
div
dl
dt
em
embed
fieldSet
form
frame
head
hn
hr
html
i
iframe
img
input type=button
input type=checkbox
input type=email
input type=file
input type=hidden
input type=image
input type=number
input type=password
input type=radio
input type=range
input type=reset
input type=search
input type=submit
input type=tel
input type=text
input type=url
kbd
label
legend
li
link
map
menu
meta
object
ol
option
p
pre
runtimeStyle
s
samp
script
select
small
span
strong
style
sub
sup
table
tBody
td
textArea
tFoot
th
tHead
title
tr
u
ul
var
Reference
getAttribute
removeAttribute

 

 

Afficher:
© 2015 Microsoft