Наши волонтёры ещё не перевели данную статью на Русский. Присоединяйтесь к нам и помогите сделать эту работу!
Вы можете также прочитать эту статью на English (US).

The Document.createAttribute() method creates a new attribute node, and returns it. The object created a node implementing the Attr interface. The DOM does not enforce what sort of attributes can be added to a particular element in this manner.

The string given in parameter is converted to lowercase.

Syntax

attribute = document.createAttribute(name) 

Parameters

  • name is a string containing the name of the attribute.

Return value

A Attr node.

Exceptions

  • INVALID_CHARACTER_ERR if the parameter contains invalid characters for XML attribute.

Example

var node = document.getElementById("div1");
var a = document.createAttribute("my_attrib");
a.value = "newVal";
node.setAttributeNode(a);
console.log(node.getAttribute("my_attrib")); // "newVal"

Specifications

Specification Status Comment
DOM
The definition of 'Document.createAttribute()' in that specification.
Living Standard Precised behavior with uppercase characters
Document Object Model (DOM) Level 3 Core Specification
The definition of 'Document.createAttribute()' in that specification.
Obsolete No change.
Document Object Model (DOM) Level 2 Core Specification
The definition of 'Document.createAttribute()' in that specification.
Obsolete No change.
Document Object Model (DOM) Level 1 Specification
The definition of 'Document.createAttribute()' in that specification.
Obsolete Initial definition.

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
Basic supportChrome Full support YesEdge Full support YesFirefox Full support 44
Full support 44
No support 4 — 44
Notes
Notes The parameter was not converted to its lowercase variant.
IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 44
Full support 44
No support 4 — 44
Notes
Notes The parameter was not converted to its lowercase variant.
Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android ?

Legend

Full support  
Full support
Compatibility unknown  
Compatibility unknown
See implementation notes.
See implementation notes.

See also

Метки документа и участники

Метки: 
Внесли вклад в эту страницу: fscholz, erikadoyle, teoli, kosvrouvas, evinugur, kscarfone, Cobra, Sheppy, ethertank, ziyunfei, Mgjbot, Nickolay, Jabez, Takenbot, BenoitL, Callek, Hao2lian, Dria, JesseW
Обновлялась последний раз: fscholz,