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

aria-hidden และ aria-live

ซ่อนองค์ประกอบตกแต่งและประกาศเนื้อหาแบบไดนามิกให้โปรแกรมอ่านหน้าจอทราบ

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

แอตทริบิวต์ aria-hidden

aria-hidden="true" จะนำองค์ประกอบออกจากแผนผังการเข้าถึง ทำให้โปรแกรมอ่านหน้าจอข้ามองค์ประกอบนั้นไปโดยสิ้นเชิง:

<!-- Decorative icon: visible but skip by screen readers -->
<span aria-hidden="true">★★★★☆</span>
<span class="sr-only">4 out of 5 stars</span>

<!-- Decorative separator -->
<img src="divider.svg" alt="" aria-hidden="true">

<!-- Note: aria-hidden does NOT affect visibility or tab focus
     A button with aria-hidden is still tabbable! -->

กรณีการใช้งาน aria-hidden

กรณีที่เหมาะสมสำหรับการใช้ aria-hidden:

<!-- 1. Icons next to descriptive text -->
<button>
  <svg aria-hidden="true"><!-- icon --></svg>
  Save Document
</button>

<!-- 2. Repeated text in a card link -->
<a href="/blog/post-1">
  <span aria-hidden="true">Read more</span>
  <span class="sr-only">Read more: HTML5 Forms Guide</span>
</a>

<!-- 3. UI decoration (dots, decorative borders) -->
<span aria-hidden="true">•••</span>

ข้อผิดพลาดของ aria-hidden

อย่าใช้ aria-hidden กับองค์ประกอบที่รับโฟกัสได้โดยเด็ดขาด:

<!-- BAD: button is in tab order but invisible to screen readers -->
<button aria-hidden="true">Close</button>

<!-- Users reach this via Tab but hear nothing
     They cannot tell if they're on a button or lost focus -->

<!-- If you want to hide from both: use hidden attribute -->
<button hidden>Close</button>

<!-- Or: disabled (but disabled removes from tab order) -->
<button disabled aria-hidden="true">Close</button>

แอตทริบิวต์ aria-live

aria-live จะเปลี่ยนพื้นที่หนึ่งให้เป็น พื้นที่ประกาศแบบสด โดยระบบจะประกาศการเปลี่ยนแปลงโดยอัตโนมัติ:

<!-- polite: announce when user is idle -->
<div aria-live="polite" id="status"></div>

<!-- assertive: announce immediately, interrupting current speech -->
<div aria-live="assertive" id="errors"></div>

<script>
// Inject text to trigger announcement:
document.getElementById('status').textContent = 'File saved successfully.';
// Screen reader announces this without user moving focus
</script>

ค่าของ aria-live

ค่า aria-live ทั้งสามค่า:

  • off — ค่าเริ่มต้น: ไม่ประกาศการเปลี่ยนแปลง
  • polite — ประกาศเมื่อผู้ใช้หยุดพัก เหมาะสำหรับการอัปเดตที่ไม่เร่งด่วน
  • assertive — ประกาศทันทีและขัดจังหวะ เหมาะสำหรับข้อผิดพลาดและการแจ้งเตือนสำคัญ

แอตทริบิวต์ aria-atomic

aria-atomic ควบคุมว่าจะประกาศทั้งพื้นที่หรือเฉพาะส่วนที่เปลี่ยนแปลง:

<div aria-live="polite" aria-atomic="true" id="timer">
  Time remaining: 05:00
</div>
<!-- aria-atomic="true": announce the whole div each update -->
<!-- Without it: only the changed text node is announced
     'Time remaining: 05:00' vs just '05:00' -->

<script>
setInterval(() => {
  document.getElementById('timer').textContent = `Time remaining: ${getTime()}`;
}, 1000);
</script>

แอตทริบิวต์ aria-relevant

aria-relevant ระบุประเภทของการเปลี่ยนแปลงที่จะประกาศ:

<div
  aria-live="polite"
  aria-relevant="additions text"   <!-- announce added content and text changes -->
  id="feed"
>
  <!-- New items added here -->
</div>
<!-- Values: additions, removals, text, all
     Default: additions text
     aria-relevant="removals" needed to announce deleted items -->

แนวทางปฏิบัติที่ดีสำหรับพื้นที่ประกาศแบบสด

แนวทางสำหรับพื้นที่ประกาศแบบสด:

  • ใช้พื้นที่ประกาศแบบสดที่คงอยู่ถาวรหนึ่งพื้นที่ต่อการประกาศแต่ละประเภท (อย่าสร้างแล้วลบทิ้ง)
  • เริ่มต้นด้วยเนื้อหาว่าง แล้วแทรกข้อความเพื่อเรียกใช้การประกาศ
  • ประกาศให้กระชับ เพราะโปรแกรมอ่านหน้าจออาจขัดจังหวะหรืออ่านตามเนื้อหาไม่ทัน
  • สำหรับสถานะกำลังโหลด ให้ใช้ polite
  • สำหรับข้อผิดพลาดของแบบฟอร์ม ให้ใช้ assertive หรือ role="alert"

รูปแบบสำหรับการแจ้งเตือนแบบ Toast

การประกาศการแจ้งเตือนแบบ Toast ด้วยพื้นที่ประกาศแบบสด:

<div
  role="status"
  aria-live="polite"
  aria-atomic="true"
  id="toast-container"
  class="sr-only"
></div>

<script>
function showToast(message) {
  const container = document.getElementById('toast-container');
  container.textContent = '';
  // Force rerender for re-announcement:
  requestAnimationFrame(() => {
    container.textContent = message;
  });
}
</script>

แอตทริบิวต์ aria-busy

aria-busy="true" ระบุว่าเนื้อหากำลังได้รับการอัปเดต:

<div id="feed" aria-live="polite" aria-busy="false">
  <!-- content -->
</div>

<script>
const feed = document.getElementById('feed');
feed.setAttribute('aria-busy', 'true');

await loadContent();
feed.innerHTML = newContent;

feed.setAttribute('aria-busy', 'false');
// Only now does the live region announce the update
</script>

ตรวจสอบความเข้าใจอย่างรวดเร็ว

ควรใช้ aria-live="assertive" แทนที่จะเป็น "polite" เมื่อใด

สรุป: aria-hidden และ aria-live

สาระสำคัญของการซ่อนและพื้นที่ประกาศแบบสด:

  • aria-hidden="true" — นำออกจากแผนผังการเข้าถึง (ห้ามใช้กับองค์ประกอบที่รับโฟกัสได้!)
  • aria-live="polite" — ประกาศการเปลี่ยนแปลงเมื่อผู้ใช้ไม่ได้ทำกิจกรรม
  • aria-live="assertive" — ประกาศทันที (สำหรับข้อผิดพลาด)
  • aria-atomic="true" — ประกาศทั้งพื้นที่เมื่อมีการเปลี่ยนแปลง
  • aria-busy — ระงับการประกาศระหว่างการโหลด

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

บทเรียน “aria-hidden และ aria-live” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “aria-hidden และ aria-live”

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

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

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

บทเรียน “aria-hidden และ aria-live” ใช้เวลานานแค่ไหน

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

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

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

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

  1. เมื่อใดควรใช้ ARIA เทียบกับ HTML แบบเนทีฟ
  2. role button alert dialog และ landmark
  3. aria-label aria-labelledby และ aria-describedby
  4. aria-hidden และ aria-live
← กลับไปที่ HTML Academy