Fokus im Modal einschließen
Verhindern Sie, dass Tab auf die dahinterliegende Seite entkommt.
Fokus im Modal einschließen ist eine kostenlose Web Accessibility Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Web Accessibility Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Web Accessibility Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
Why Focus Must Stay In
While a modal is open, keyboard focus should stay inside it. If Tab escapes to the page behind, blind users get lost in content they cannot see.
The Native Element Helps
A real dialog opened with showModal() traps focus for you automatically, which is one big reason to prefer it over a div.
When You Build Your Own
If you roll a custom modal with a div, you must trap focus yourself, because nothing stops Tab from leaking out to the page.
Move Focus In on Open
First, send focus into the modal when it opens, usually to the first control or the heading, so the user starts in the right place.
modal.querySelector('button').focus();Find the Focusable Elements
Collect the modal's focusable items: links, buttons, inputs, and anything with tabindex. You need the first and last to build the loop.
modal.querySelectorAll('a, button, input, [tabindex]');Loop From Last to First
When Tab is pressed on the last element, send focus back to the first. This wraps the user around instead of letting them out.
if (e.key==='Tab' && !e.shiftKey && active===last){
e.preventDefault(); first.focus();
}Loop From First to Last
Handle Shift+Tab on the first element too: send focus to the last one so backward tabbing also stays trapped.
if (e.key==='Tab' && e.shiftKey && active===first){
e.preventDefault(); last.focus();
}Prevent the Default Tab
At the edges you must call preventDefault(), otherwise the browser moves focus out before your code can redirect it.
e.preventDefault();Mind Disabled and Hidden Items
Skip elements that are disabled or hidden when picking first and last, since they cannot actually receive focus.
Do Not Forget the Background
Trapping focus pairs with making the page behind inert, so even a stray click or screen reader cannot reach it while the modal is open.
Test It With the Keyboard
Tab through the whole modal and watch the focus ring cycle without ever leaving. That round trip is your proof the trap works. ✅
Quick Check
You built a custom div modal. What must you do to keep Tab inside it?
Recap: Keep Focus Captive
You learned to move focus in on open and loop it at both edges, so keyboard users stay inside the modal until they choose to close it. 🎯
Häufig gestellte Fragen
Ist die Lektion „Fokus im Modal einschließen“ kostenlos?
Ja — der vollständige Text von „Fokus im Modal einschließen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Web Accessibility Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Web Accessibility Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Fokus im Modal einschließen“?
Verhindern Sie, dass Tab auf die dahinterliegende Seite entkommt. Du übst Web Accessibility Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Web Accessibility Academy zu starten?
Keine Vorkenntnisse erforderlich. Web Accessibility Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.
Wie lange dauert die Lektion „Fokus im Modal einschließen“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Web Accessibility Academy-Lektion Code schreiben und ausführen?
Ja. Jede Web Accessibility Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Dialogrollen und das native dialog-Element
- Fokus im Modal einschließen
- Mit Escape schließen und den Fokus wiederherstellen
- Inaktiver Hintergrund und aria-modal