DOM 要素を作るためのヘルパー関数です。HTML と SVG のどちらの要素にも対応します。
| 引数 |
説明 |
型 |
HTMLElement |
作られた DOM 要素 |
HTMLElement |
| パラメーター |
説明 |
型 |
既定値 |
tagName |
タグ名 |
string |
必須 |
options |
生成のオプション(任意) |
ElementCreationOptions |
任意 |
import { create } from 'ranuts';
const div = create('div');
div.textContent = 'Hello World';
document.body.appendChild(div);
import { create } from 'ranuts';
const svg = create('svg');
svg.setAttribute('width', '100');
svg.setAttribute('height', '100');
const circle = create('circle');
circle.setAttribute('cx', '50');
circle.setAttribute('cy', '50');
circle.setAttribute('r', '40');
svg.appendChild(circle);
import { create } from 'ranuts';
// カスタム要素を作る
const customElement = create('my-custom-element', { is: 'my-element' });
- 自動で見分けます:SVG のタグを自動で見分け、正しい名前空間で作ります。
- HTML 要素:普通の HTML 要素は
document.createElement で作ります。
- SVG 要素:SVG 要素は
document.createElementNS で作ります。
- 使いどころ:SVG 要素を作る必要がある場面でよく使われ、生成の手順を短くします。