diff --git a/static/common.js b/static/common.js index 7c98781..64c272c 100644 --- a/static/common.js +++ b/static/common.js @@ -3,6 +3,12 @@ (function (global) { "use strict"; + // 布尔属性:用属性赋值而非 setAttribute(setAttribute("checked","false") 仍会勾选) + const BOOL_PROPS = new Set([ + "checked", "disabled", "readonly", "selected", "hidden", "multiple", "open", + "autofocus", "required", "async", "defer", "controls", "autoplay", "loop", "muted", + ]); + function el(tag, attrs, ...children) { const node = document.createElement(tag); if (attrs) { @@ -11,7 +17,8 @@ else if (k === "dataset") Object.assign(node.dataset, v); else if (k.startsWith("on") && typeof v === "function") node.addEventListener(k.slice(2).toLowerCase(), v); - else if (v !== null && v !== undefined) node.setAttribute(k, v); + else if (BOOL_PROPS.has(k)) node[k] = !!v; + else if (v !== null && v !== undefined && v !== false) node.setAttribute(k, v); } } for (const c of children) {