fix: 复选框初始状态错误(el 对布尔属性用 setAttribute 导致 checked:false 仍勾选)
common.js 的 el() 对 checked/disabled/hidden 等布尔属性调用 setAttribute(k, v),
而 setAttribute("checked", "false") 仍会令 checkbox 呈勾选态(属性存在即为真)。
改为维护 BOOL_PROPS 集合,布尔属性用 node[k] = !!v 赋值;同时 v === false 时跳过 setAttribute。
修复后文件浏览页复选框初始未选中时正确显示为未勾选。
This commit is contained in:
@@ -3,6 +3,12 @@
|
|||||||
(function (global) {
|
(function (global) {
|
||||||
"use strict";
|
"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) {
|
function el(tag, attrs, ...children) {
|
||||||
const node = document.createElement(tag);
|
const node = document.createElement(tag);
|
||||||
if (attrs) {
|
if (attrs) {
|
||||||
@@ -11,7 +17,8 @@
|
|||||||
else if (k === "dataset") Object.assign(node.dataset, v);
|
else if (k === "dataset") Object.assign(node.dataset, v);
|
||||||
else if (k.startsWith("on") && typeof v === "function")
|
else if (k.startsWith("on") && typeof v === "function")
|
||||||
node.addEventListener(k.slice(2).toLowerCase(), v);
|
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) {
|
for (const c of children) {
|
||||||
|
|||||||
Reference in New Issue
Block a user