优化 createElement
This commit is contained in:
parent
e9f891cf15
commit
ec64da1f37
@ -1,15 +1,13 @@
|
|||||||
export function createElement({ innerText, textContent, onclick, onchange, children = [], dataset, style, classList = [], ...attributes }, tagName = 'div') {
|
export function createElement({ innerText, textContent, onclick, onchange, children = [], dataset, style, classList = [], ...attributes }, tagName = 'div') {
|
||||||
const element = document.createElement(tagName)
|
const element = document.createElement(tagName)
|
||||||
for (const key in attributes) {
|
for (const key in attributes) element.setAttribute(key, attributes[key])
|
||||||
element.setAttribute(key, attributes[key])
|
|
||||||
}
|
|
||||||
if (style) Object.assign(element.style, style)
|
if (style) Object.assign(element.style, style)
|
||||||
if (classList.length) element.classList.add(...classList)
|
if (classList.length) element.classList.add(...classList)
|
||||||
if (innerText) element.innerText = innerText
|
|
||||||
if (textContent) element.textContent = textContent
|
if (textContent) element.textContent = textContent
|
||||||
if (onclick) element.onclick = onclick
|
if (innerText) element.innerText = innerText
|
||||||
if (onchange) element.onchange = onchange
|
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))
|
if (children) children.forEach(child => element.appendChild(child))
|
||||||
return element
|
return element
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user