创建 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 元素的场景,简化创建过程。