Document.createAttribute() - 属性ノードを作成する
createAttribute()は、Documentのメソッドです。属性ノード(Node)を新しく作成します。
概要
- 名前
- createAttribute
- 所属
- Document
- IDL
[NewObject] Attr createAttribute(DOMString localName);
- 仕様書
- https://dom.spec.whatwg.org/#dom-document-createattribute
説明
引数(localName)には、属性名を指定します。
チュートリアル
属性ノードを作成する例です。Element.setAttribute()などの方が便利なので、ほとんど使う機会はありません。
var attr = document.createAttribute( "width" ) ; // width=""
attr.value = 300 ; // width="300"
element.setAttributeNode( attr ) ;
デモ
Document.createAttribute()のデモです。
<!-- このコードは編集できます。 -->
<!DOCTYPE html>
<html>
<body>
<script>
var attr = document.createAttribute( "width" ) ;
console.log( attr ) ;
document.body.appendChild( new Text( attr ) ) ;
</script>
</body>
</html>
サポート状況
Chrome | Firefox | Safari | Edge | IE | Opera | iOS Safari | Android |
---|---|---|---|---|---|---|---|
● | ● | ● | ● | ● | ● | ● | ● |
関連記事
- Selection.addRange()
- addRange()は、Selectionのメソッドです。Rangeを指定して選択範囲をセットします。既に選択範囲が存在する場合、無視されます。
- Selection.type
- typeは、Selectionのプロパティです。選択範囲の種類を文字列で返します。
- Range.startOffset
- startOffsetは、Rangeのプロパティです。始点の位置を返します。
- Selection.focusNode
- focusNodeは、Selectionのプロパティです。選択範囲の終点を含むノードを返します。