MDN wants to learn about developers like you: https://qsurvey.mozilla.com/s3/MDN-survey

この記事はまだボランティアによって 日本語 に翻訳されていません。ぜひ MDN に参加して翻訳を手伝ってください!
この記事を 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

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!

  
Feature Firefox (Gecko) Chrome Edge Internet Explorer Opera Safari
Basic support 1.0 (1.7 or earlier)[1] (Yes)(Yes) (Yes) (Yes) (Yes)
  
Feature Firefox Mobile (Gecko) Android Edge IE Mobile Opera Mobile Safari Mobile
onselectionchange 1.0 (1)[1] (Yes)(Yes) (Yes) (Yes) (Yes)

[1] Before Firefox 44, the parameter wasn't converted to its lowercase variant.

See also

ドキュメントのタグと貢献者

タグ: 
 最終更新者: erikadoyle,