30天挑戰:「為什麼這個input沒有觸發@blur="event"」問題解析
原始問題
- 使用
input:checkbox
搭配label
來製作 light/dark mode 的切換按鈕 input:checkbox
使用display: none;
隱藏於畫面中,而label
與input
綁定,故點擊label
可勾選(取消勾選)該input:checkbox
- 想要為該
input
設定@blur
事件,但怎麼嘗試都無法讓該元素進入blur
狀態
解答
An element is focusable if the user agent’s default behavior allows it to be focusable or if the element is specially focusable, but only if the element is either being rendered or is a descendant of a canvas element that represents embedded content.
- 一個元素要被渲染到畫面上,他才能被聚焦(失焦)
- 若把該元素設定為
display: none;
,他就不會被渲染到畫面上,故也不會有聚焦(失焦)行為
補充
User agents should make the following elements focusable, unless platform conventions dictate otherwise:
a
elements that have anhref
attributelink
elements that have anhref
attributebutton
elements that are not disabledinput
elements whose type attribute are not in the Hidden state and that are not disabledselect
elements that are not disabledtextarea
elements that are not disabledcommand
elements that do not have a disabled attribute- Elements with a
draggable
attribute set, if that would enable the user agent to allow the user to begin a drag operations for those elements without the use of a pointing device - Editing hosts
- If an element is editable and its parent element is not, or if an element is editable and it has no parent element, then the element is an editing host.
- Editable elements can be nested.
- Editing hosts are typically focusable and typically form part of the tab order.
- An editing host can contain non-editable sections, these are handled as described below.
- An editing host can contain non-editable sections that contain further editing hosts.
- In addition, each shape that is generated for an
area
element should be focusable, unless platform conventions dictate otherwise.