操作后移动焦点
将焦点移到用户下一步需要的位置。
操作后移动焦点 是 CoddyKit 上的免费 Web Accessibility Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Web Accessibility Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Web Accessibility Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Focus Is the User's Cursor
For keyboard and screen reader users, focus is where they are on the page. After an action, you decide where that point lands next.
Why Move Focus at All
When a button reveals new content, focus often stays put and the user never learns anything changed. Moving focus brings them to the result.
The focus Method
You move focus in JavaScript by calling element.focus() on the target you want the user to land on next.
document.getElementById('results').focus();Make the Target Focusable First
A plain div will not accept focus, so give the destination tabindex="-1" so your script can move focus to it.
<div id="results" tabindex="-1">3 matches found</div>Example: Reveal and Focus
After loading new content, show it and then send focus there so the screen reader starts reading the new region. 🔎
panel.hidden = false;
panel.focus();Focus the Heading, Not the Whole Region
Pointing focus at the new section's heading often reads best, giving the user a clear title for where they just arrived.
Deleting an Item
When the user removes a row, focus would otherwise be lost. Move it to the next item or a nearby control so they can keep going.
Avoid the Focus-to-Top Jolt
Letting focus snap back to the page top after every action is jarring. Aim it at the relevant spot instead so context is preserved.
Do Not Steal Focus Randomly
Only move focus in response to a user action. Grabbing it during background updates yanks people out of whatever they were doing.
Confirm It Worked
Test by triggering the action with the keyboard and watching the focus ring land where you intended, every single time.
One Move Per Action
Each meaningful action should result in exactly one deliberate focus move. Predictable beats clever every time.
Quick Check
You want to move focus to a freshly shown div after a button click. What must the div have?
Recap
You learned to move focus on purpose: make the target focusable with tabindex="-1", call focus() after the action, and aim for the most useful spot. ✨
常见问题解答
「操作后移动焦点」课时是免费的吗?
是的 — 「操作后移动焦点」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Web Accessibility Academy 课程的其余内容,请升级到 CoddyKit PRO。 Web Accessibility Academy 课程共包含 4 节课。
「操作后移动焦点」这节课中我会学到什么?
将焦点移到用户下一步需要的位置。 你通过在浏览器中直接运行的动手代码来练习 Web Accessibility Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Web Accessibility Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Web Accessibility Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「操作后移动焦点」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Web Accessibility Academy 课中编写并运行代码吗?
能。每节 Web Accessibility Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。