0Pricing
HTML Academy · บทเรียน

tabindex และ accesskey

ควบคุมลำดับการนำทางด้วยแป้นพิมพ์และปุ่มลัด

tabindex และ accesskey เป็นบทเรียน HTML Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน HTML Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส HTML Academy มีบทเรียนทั้งหมด 4 บทเรียน

การนำทางด้วยแป้นพิมพ์

ผู้ใช้ที่ไม่มีเมาส์ต้องใช้ Tab เพื่อนำทางไปยังองค์ประกอบแบบโต้ตอบ:

  • องค์ประกอบที่โฟกัสได้ตามธรรมชาติ: <a href>, <button>, <input>, <select>, <textarea>
  • องค์ประกอบที่ไม่โต้ตอบ (<div>, <span>) จะไม่อยู่ในลำดับการกด Tab
  • tabindex ควบคุมว่าองค์ประกอบจะปรากฏในลำดับการกด Tab หรือไม่และปรากฏที่ใด

tabindex="0"

tabindex="0" เพิ่มองค์ประกอบที่ไม่โต้ตอบลงในลำดับการกด Tab ตามธรรมชาติ:

<div tabindex="0" role="button" onclick="handleClick()">
  Custom Button
</div>
<!-- Now Tab navigates to this div -->
<!-- It receives focus at the natural position in the DOM -->

<!-- Better: use a real button instead! -->
<button onclick="handleClick()">Real Button</button>

tabindex="-1"

tabindex="-1" ทำให้องค์ประกอบโฟกัสได้ด้วยโปรแกรม แต่ไม่นำองค์ประกอบนั้นไว้ในลำดับการกด Tab:

<div id="modal" tabindex="-1" role="dialog">
  <!-- modal content -->
</div>

<script>
// Open modal and send focus to it:
document.getElementById('modal').focus();
// Modal is now focused but not in the normal tab sequence
</script>

ค่าบวกของ tabindex — ควรหลีกเลี่ยง

ค่าบวกของ tabindex (เช่น tabindex="3") กำหนดลำดับการกด Tab แบบกำหนดเอง จึงควรหลีกเลี่ยง:

<!-- BAD: positive tabindex creates unpredictable order -->
<input tabindex="3">
<input tabindex="1">
<input tabindex="2">

<!-- The tab order becomes: 1, 2, 3, then all tabindex=0 elements -->
<!-- This almost always confuses users and fails WCAG 2.4.3 -->

<!-- Fix the DOM order instead of using positive tabindex -->

การจัดการโฟกัสในแอปแบบหน้าเดียว

แอปแบบหน้าต้องจัดการโฟกัสด้วยตนเอง:

// When navigating to a new route:
document.querySelector('#main-content').focus();

// When opening a modal:
const modal = document.getElementById('modal');
modal.removeAttribute('hidden');
modal.querySelector('[autofocus], button, [tabindex]').focus();

// When closing a modal: return focus to the trigger
document.getElementById('open-btn').focus();

การกักโฟกัสในโมดัล

กักโฟกัสไว้ภายในโมดัลที่เปิดอยู่ เพื่อให้ Tab ยังคงอยู่ภายในโมดัล:

function trapFocus(element) {
  const focusable = element.querySelectorAll(
    'a, button, input, textarea, select, [tabindex]:not([tabindex="-1"])'
  );
  const first = focusable[0];
  const last = focusable[focusable.length - 1];

  element.addEventListener('keydown', e => {
    if (e.key === 'Tab') {
      if (e.shiftKey && document.activeElement === first) {
        e.preventDefault(); last.focus();
      } else if (!e.shiftKey && document.activeElement === last) {
        e.preventDefault(); first.focus();
      }
    }
  });
}

แอตทริบิวต์ accesskey

แอตทริบิวต์ accesskey กำหนดแป้นพิมพ์ลัดให้กับองค์ประกอบ:

<button accesskey="s">Submit</button>
<!-- On Windows: Alt+S focuses/activates the button -->
<!-- On Mac: Control+Option+S -->
<!-- On Firefox: Shift+Alt+S -->

<a href="/" accesskey="h">Home</a>

ข้อจำกัดของ accesskey

accesskey มีปัญหาสำคัญหลายประการ:

  • ปุ่มที่ใช้เรียกการทำงานแตกต่างกันไปตามเบราว์เซอร์และระบบปฏิบัติการ
  • อาจขัดแย้งกับแป้นพิมพ์ลัดของเบราว์เซอร์ (เช่น Alt+F = เมนูไฟล์)
  • โดยค่าเริ่มต้น โปรแกรมอ่านหน้าจอจะไม่ประกาศให้ทราบ
  • แทบไม่ค่อยใช้จริง หากใช้ควรจัดทำเอกสารกำกับไว้

ตัวบ่งชี้โฟกัสที่มองเห็นได้

อย่าซ่อนเส้นขอบโฟกัส เพราะจำเป็นอย่างยิ่งสำหรับผู้ใช้แป้นพิมพ์:

/* BAD: removes focus outline for everyone */
* { outline: none; }

/* Better: style focus outlines, don't remove them */
:focus-visible {
  outline: 2px solid #0070f3;
  outline-offset: 2px;
  border-radius: 3px;
}

/* :focus-visible only shows outline for keyboard navigation -->
/* Mouse clicks do not trigger :focus-visible in modern browsers */

รูปแบบลิงก์ข้ามเนื้อหา

ลิงก์ข้ามเนื้อหาช่วยให้ผู้ใช้แป้นพิมพ์ข้ามการนำทางที่ซ้ำกันได้:

<a href="#main-content" class="skip-link">Skip to main content</a>

<nav>... long navigation ...</nav>

<main id="main-content" tabindex="-1">... content ...</main>

<!-- CSS: -->
<!--
.skip-link {
  position: absolute;
  top: -100%;
}
.skip-link:focus {
  top: 0;
}
-->

แอตทริบิวต์ autofocus

แอตทริบิวต์ autofocus ย้ายโฟกัสไปยังองค์ประกอบเมื่อโหลดหน้าเว็บ:

<input type="search" autofocus placeholder="Search...">
<!-- Useful on search pages or forms with a single primary field -->
<!-- Use sparingly: it can disrupt users who rely on the page top for orientation -->
<!-- Only one element per page should have autofocus -->

ตรวจสอบความเข้าใจ

ค่า tabindex ใดทำให้องค์ประกอบโฟกัสได้ผ่าน JavaScript แต่ถูกข้ามระหว่างการนำทางด้วย Tab ตามปกติ

ทบทวน: tabindex และ accesskey

สาระสำคัญของการเข้าถึงด้วยแป้นพิมพ์:

  • tabindex="0" — เพิ่มลงในลำดับการกด Tab ตามธรรมชาติ
  • tabindex="-1" — โฟกัสได้ด้วยโปรแกรม แต่ไม่อยู่ในลำดับการกด Tab
  • หลีกเลี่ยงค่าบวกของ tabindex
  • อย่านำเส้นขอบของ :focus-visible ออก
  • ใช้ลิงก์ข้ามเนื้อหาสำหรับผู้ใช้แป้นพิมพ์
  • accesskey ใช้งานได้ แต่ไม่ค่อยเหมาะในทางปฏิบัติเนื่องจากอาจเกิดความขัดแย้ง

คำถามที่พบบ่อย

บทเรียน “tabindex และ accesskey” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “tabindex และ accesskey” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส HTML Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส HTML Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “tabindex และ accesskey”

ควบคุมลำดับการนำทางด้วยแป้นพิมพ์และปุ่มลัด คุณปฏิบัติ HTML Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน HTML Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน HTML Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “tabindex และ accesskey” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน HTML Academy นี้ได้ไหม

ได้ บทเรียน HTML Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. id, class, style และ title
  2. แอตทริบิวต์ hidden
  3. tabindex และ accesskey
  4. แอตทริบิวต์ข้อมูลกำหนดเอง data-*
← กลับไปที่ HTML Academy