From ff2f6c7e3c408b4360133541922a8dff6b73c193 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A7=89?= Date: Mon, 2 Oct 2023 22:28:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=88=E5=B9=B6=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/weigets.js | 50 ++++++++++++----------------------------------- 1 file changed, 12 insertions(+), 38 deletions(-) diff --git a/public/weigets.js b/public/weigets.js index b72bcbb..af431d6 100644 --- a/public/weigets.js +++ b/public/weigets.js @@ -1,5 +1,5 @@ -export function List({ innerText, textContent, onclick, children = [], dataset, classList = [], ...attributes }) { - const element = document.createElement('ul') +export function createElement({ innerText, textContent, onclick, children = [], dataset, classList = [], ...attributes }, tagName) { + const element = document.createElement(tagName) for (const key in attributes) { element.setAttribute(key, attributes[key]) } @@ -12,44 +12,18 @@ export function List({ innerText, textContent, onclick, children = [], dataset, return element } -export function ListItem({ innerText, textContent, onclick, children = [], dataset, classList = [], ...attributes }) { - const element = document.createElement('li') - for (const key in attributes) { - element.setAttribute(key, attributes[key]) - } - element.classList.add(...classList) - if (innerText) element.innerText = innerText - if (textContent) element.textContent = textContent - if (onclick) element.onclick = onclick - if (dataset) Object.keys(dataset).forEach(key => element.dataset[key] = dataset[key]) - if (children) children.forEach(child => element.appendChild(child)) - return element +export function List(options) { + return createElement(options, 'ul') } -export function Span({ innerText, textContent, onclick, children = [], dataset, classList = [], ...attributes }) { - const element = document.createElement('span') - for (const key in attributes) { - element.setAttribute(key, attributes[key]) - } - element.classList.add(...classList) - if (innerText) element.innerText = innerText - if (textContent) element.textContent = textContent - if (onclick) element.onclick = onclick - if (dataset) Object.keys(dataset).forEach(key => element.dataset[key] = dataset[key]) - if (children) children.forEach(child => element.appendChild(child)) - return element +export function ListItem(options) { + return createElement(options, 'li') } -export function Button({ innerText, textContent, onclick, children = [], dataset, classList = [], ...attributes }) { - const element = document.createElement('button') - for (const key in attributes) { - element.setAttribute(key, attributes[key]) - } - element.classList.add(...classList) - if (innerText) element.innerText = innerText - if (textContent) element.textContent = textContent - if (onclick) element.onclick = onclick - if (dataset) Object.keys(dataset).forEach(key => element.dataset[key] = dataset[key]) - if (children) children.forEach(child => element.appendChild(child)) - return element +export function Span(options) { + return createElement(options, 'span') +} + +export function Button(options) { + return createElement(options, 'button') }