حصر التركيز داخل النافذة المشروطة
امنعوا Tab من الخروج إلى الصفحة الموجودة خلفها
حصر التركيز داخل النافذة المشروطة درس مجاني في Web Accessibility Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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/7) وفتح باقي دورة Web Accessibility Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Web Accessibility Academy 4 دروس في المجموع.
ماذا ستتعلم في «حصر التركيز داخل النافذة المشروطة»؟
امنعوا Tab من الخروج إلى الصفحة الموجودة خلفها تتمرن على Web Accessibility Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Web Accessibility Academy؟
لا تُشترط خبرة سابقة. Web Accessibility Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «حصر التركيز داخل النافذة المشروطة»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Web Accessibility Academy هذا؟
نعم. كل درس في Web Accessibility Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- أدوار مربعات الحوار وعنصر dialog الأصلي
- حصر التركيز داخل النافذة المشروطة
- الإغلاق بمفتاح Escape واستعادة التركيز
- الخلفية غير النشطة وaria-modal