disabled属性是布尔属性,存在即禁用,启用必须设为false或移除属性;推荐直接操作element.disabled = false,而非removeattribute或错误赋值字符串。

disabled 属性不是“取消”,而是设为 false 或移除
HTML 中 disabled 是布尔属性,只要存在(哪怕值是 disabled="false"),元素就处于禁用状态。所以“动态取消”本质是:把 disabled 属性删掉,或把 DOM 元素的 disabled 属性设为 false —— 二者效果等价,但推荐后者。
常见错误是写 button.disabled = "false" 或 button.setAttribute("disabled", "false"),这反而会启用禁用逻辑(因为属性存在)。
-
button.disabled = true→ 禁用(推荐,直接操作属性) -
button.disabled = false→ 启用(推荐) -
button.removeAttribute("disabled")→ 启用(可行,但略冗余) -
button.setAttribute("disabled", "")或"disabled"→ 禁用
用 JavaScript 控制时,必须确保 DOM 已加载
如果脚本在 <button></button> 标签前执行,document.getElementById() 会返回 null,后续赋值报错:Cannot set property 'disabled' of null。
- 把
<script></script>放在
前端入门到VUE实战笔记:立即使用
在学习笔记中,你将探索 前端 的入门与实战技巧!











