From 5a39a2eb3d88a5ac839dc2678ee5a350cc301cbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A7=89?= Date: Fri, 13 Oct 2023 03:10:39 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E4=B8=BA=20element=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=89=80=E6=9C=89=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/weigets.js | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/public/weigets.js b/public/weigets.js index d780887..c5ad285 100644 --- a/public/weigets.js +++ b/public/weigets.js @@ -1,21 +1,16 @@ -export function createElement({ innerText, textContent, onclick, onchange, ondrop, ondragover, ondragleave, onkeydown, onmouseenter, onmouseleave, readOnly, children = [], dataset, style, classList = [], ...attributes }, tagName = 'div') { +export function createElement({ innerText, textContent, readOnly, 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) { + if (key.startsWith('on')) element[key] = attributes[key] // 如果是事件则直接赋值 + else element.setAttribute(key, attributes[key]) // 否则是属性则使用setAttribute + } if (style) Object.assign(element.style, style) if (classList.length) element.classList.add(...classList) if (textContent) element.textContent = textContent if (innerText) element.innerText = innerText if (readOnly) element.readOnly = readOnly - if (onkeydown) element.onkeydown = onkeydown - if (onchange) element.onchange = onchange - if (onclick) element.onclick = onclick if (dataset) Object.assign(element.dataset, dataset) if (children) children.forEach(child => element.appendChild(child)) - if (onmouseenter) element.onmouseenter = onmouseenter - if (onmouseleave) element.onmouseleave = onmouseleave - if (ondrop) element.ondrop = ondrop - if (ondragover) element.ondragover = ondragover - if (ondragleave) element.ondragleave = ondragleave return element }