0Pricing
Web Accessibility Academy · レッスン

モーダル内にフォーカスを閉じ込める

Tabキーで背後のページへフォーカスが逃げないようにします。

「モーダル内にフォーカスを閉じ込める」はCoddyKit上の無料Web Accessibility Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはWeb Accessibility Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Web Accessibility Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

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. 🎯

よくある質問

「モーダル内にフォーカスを閉じ込める」レッスンは無料ですか?

はい。「モーダル内にフォーカスを閉じ込める」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Web Accessibility Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Web Accessibility Academyコースには全4レッスンが含まれています。

「モーダル内にフォーカスを閉じ込める」で何を学びますか?

Tabキーで背後のページへフォーカスが逃げないようにします。 ブラウザで直接実行するハンズオンコードでWeb Accessibility Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Web Accessibility Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのWeb Accessibility Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。

「モーダル内にフォーカスを閉じ込める」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このWeb Accessibility Academyレッスンでコードを書いて実行できますか?

はい。すべてのWeb Accessibility Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. ダイアログロールとネイティブのdialog要素
  2. モーダル内にフォーカスを閉じ込める
  3. Escapeで閉じてフォーカスを戻す
  4. 背景をinertにしてaria-modalを設定する
← Web Accessibility Academyに戻る