优化 createElement

This commit is contained in:
2023-10-07 00:49:02 +08:00
parent e9f891cf15
commit ec64da1f37
1 changed files with 4 additions and 6 deletions

View File

@ -1,15 +1,13 @@
export function createElement({ innerText, textContent, onclick, onchange, children = [], dataset, style, classList = [], ...attributes }, tagName = 'div') {
const element = document.createElement(tagName)
for (const key in attributes) {
element.setAttribute(key, attributes[key])
}
for (const key in attributes) element.setAttribute(key, attributes[key])
if (style) Object.assign(element.style, style)
if (classList.length) element.classList.add(...classList)
if (innerText) element.innerText = innerText
if (textContent) element.textContent = textContent
if (onclick) element.onclick = onclick
if (innerText) element.innerText = innerText
if (onchange) element.onchange = onchange
if (dataset) Object.keys(dataset).forEach(key => element.dataset[key] = dataset[key])
if (onclick) element.onclick = onclick
if (dataset) Object.assign(element.dataset, dataset)
if (children) children.forEach(child => element.appendChild(child))
return element
}