ภูมิภาคแบบไลฟ์: aria-live aria-atomic aria-relevant
ประกาศการอัปเดตแบบไม่พร้อมกันให้เทคโนโลยีสิ่งอำนวยความสะดวกทราบ
ภูมิภาคแบบไลฟ์: aria-live aria-atomic aria-relevant เป็นบทเรียน HTML Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน HTML Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส HTML Academy มีบทเรียนทั้งหมด 4 บทเรียน
พื้นที่ประกาศแบบสดคืออะไร
พื้นที่ประกาศแบบสด คือส่วนหนึ่งของหน้าเว็บที่เนื้อหาอัปเดตแบบไดนามิก โปรแกรมอ่านหน้าจอจะตรวจสอบพื้นที่ประกาศแบบสดและประกาศการเปลี่ยนแปลงโดยอัตโนมัติ โดยไม่ต้องให้ผู้ใช้ย้ายโฟกัส
การสร้างพื้นที่ประกาศแบบสด
เพิ่ม aria-live ให้กับองค์ประกอบ container:
<!-- Polite: announce when user is idle -->
<div aria-live="polite" id="notifications"></div>
<!-- Inject text to announce: -->
<script>
document.getElementById('notifications').textContent = '5 new messages';
// Screen reader announces: '5 new messages' when user pauses
</script>สรุปค่า aria-live
ระดับของ aria-live ทั้งสามระดับ:
off— ไม่มีการประกาศ (ค่าเริ่มต้นสำหรับพื้นที่ที่ไม่ใช่พื้นที่ประกาศแบบสด)polite— ประกาศเมื่อผู้ใช้หยุดพูดassertive— ประกาศทันทีและขัดจังหวะเสียงพูดปัจจุบัน
เจาะลึก aria-atomic
aria-atomic ควบคุมว่าจะประกาศสิ่งใดเมื่อเนื้อหาเปลี่ยนแปลง:
<div aria-live="polite" aria-atomic="false"> <!-- default -->
Items in cart: <span id="count">3</span>
</div>
<!-- Only the changed span is announced: '4' -->
<div aria-live="polite" aria-atomic="true">
Items in cart: <span id="count">3</span>
</div>
<!-- The entire div is announced: 'Items in cart: 4' -->
<!-- aria-atomic="true" gives more context but is more verbose -->ค่า aria-relevant
aria-relevant ระบุว่าการเปลี่ยนแปลงใน DOM ประเภทใดจะเรียกใช้การประกาศ:
<!-- Default: additions text -->
<div aria-live="polite" aria-relevant="additions text">
<!-- Announce removals too: -->
<div aria-live="polite" aria-relevant="removals">
<!-- Announce everything: -->
<div aria-live="polite" aria-relevant="all">
<!-- Values: additions, removals, text, all
'additions' = added nodes announced
'removals' = removed nodes announced
'text' = text content changes announced -->บทบาทพื้นที่ประกาศแบบสดที่กำหนดไว้ล่วงหน้า
บทบาทบางอย่างจะกำหนด aria-live โดยนัย:
role="alert"— aria-live="assertive" + aria-atomic="true"role="status"— aria-live="polite" + aria-atomic="true"role="log"— aria-live="polite"role="timer"— aria-live="off"role="marquee"— aria-live="off"
สถานะกำลังโหลดด้วยพื้นที่ประกาศแบบสด
ประกาศสถานะกำลังโหลดและโหลดเสร็จแล้ว:
<div role="status" aria-live="polite" aria-atomic="true" id="load-status">
<!-- empty initially -->
</div>
<script>
async function loadData() {
document.getElementById('load-status').textContent = 'Loading...';
const data = await fetchData();
renderResults(data);
document.getElementById('load-status').textContent =
`${data.length} results loaded.`;
}
</script>ผลตอบกลับการส่งแบบฟอร์ม
พื้นที่ประกาศแบบสดสำหรับสถานะการส่งแบบฟอร์ม:
<form id="contact-form" novalidate>
<input type="email" name="email">
<button type="submit">Subscribe</button>
</form>
<div role="status" aria-live="polite" id="form-status"></div>
<script>
form.addEventListener('submit', async (e) => {
e.preventDefault();
const status = document.getElementById('form-status');
status.textContent = 'Submitting...';
await submitForm(new FormData(form));
status.textContent = 'Thank you! You are now subscribed.';
});
</script>จำนวนผลการค้นหา
ประกาศผลการค้นหาแบบไดนามิก:
<label for="search">Search</label>
<input type="search" id="search">
<div role="status" aria-live="polite" aria-atomic="true" id="results-count"></div>
<div id="results"></div>
<script>
let debounceTimer;
search.addEventListener('input', () => {
clearTimeout(debounceTimer);
debounceTimer = setTimeout(() => {
const results = filterResults(search.value);
renderResults(results);
document.getElementById('results-count').textContent =
`${results.length} results for "${search.value}"`;
}, 300);
});
</script>รูปแบบที่ควรหลีกเลี่ยงของพื้นที่ประกาศแบบสด
หลีกเลี่ยงข้อผิดพลาดเหล่านี้เกี่ยวกับพื้นที่ประกาศแบบสด:
- อย่าสร้างแล้วลบพื้นที่ประกาศแบบสด ให้พื้นที่เหล่านี้คงอยู่ถาวรโดยเริ่มต้นด้วยเนื้อหาว่าง
- อย่าวางพื้นที่ประกาศแบบสดไว้ภายในพื้นที่ aria-hidden
- อย่าใช้ assertive สำหรับการอัปเดตตามปกติ เพราะรบกวนผู้ใช้มาก
- อย่าประกาศการอัปเดตถี่เกินไป (จำกัดความถี่ของจำนวนผลการค้นหา)
การทดสอบพื้นที่ประกาศแบบสด
วิธีทดสอบการประกาศของพื้นที่ประกาศแบบสด:
- เปิด VoiceOver (Mac: Cmd+F5) หรือ NVDA (Windows: Ctrl+Alt+N)
- เรียกใช้การอัปเดตและฟังการประกาศ
- ตรวจสอบจังหวะเวลา: polite จะรอ ส่วน assertive จะขัดจังหวะ
- ตรวจสอบว่า axe DevTools ไม่รายงานปัญหาของพื้นที่ประกาศแบบสด
ตรวจสอบความเข้าใจอย่างรวดเร็ว
บทบาทใดกำหนด aria-live เป็น assertive และ aria-atomic เป็น true โดยนัย
สรุป: พื้นที่ประกาศแบบสด
สาระสำคัญของพื้นที่ประกาศแบบสด:
aria-live="polite"— รอให้ผู้ใช้หยุดพักaria-live="assertive"— ขัดจังหวะทันทีaria-atomic="true"— ประกาศทั้งพื้นที่role="alert"= assertive + atomic; role="status" = polite + atomic- ทำให้พื้นที่ประกาศแบบสดคงอยู่ถาวร ล้างเนื้อหาแล้วจึงกำหนดเนื้อหาใหม่
คำถามที่พบบ่อย
บทเรียน “ภูมิภาคแบบไลฟ์: aria-live aria-atomic aria-relevant” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ภูมิภาคแบบไลฟ์: aria-live aria-atomic aria-relevant” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส HTML Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส HTML Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ภูมิภาคแบบไลฟ์: aria-live aria-atomic aria-relevant”
ประกาศการอัปเดตแบบไม่พร้อมกันให้เทคโนโลยีสิ่งอำนวยความสะดวกทราบ คุณปฏิบัติ HTML Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน HTML Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน HTML Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “ภูมิภาคแบบไลฟ์: aria-live aria-atomic aria-relevant” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน HTML Academy นี้ได้ไหม
ได้ บทเรียน HTML Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- aria-expanded และ aria-controls สำหรับตัวสลับ
- aria-selected และรูปแบบแท็บ
- ภูมิภาคแบบไลฟ์: aria-live aria-atomic aria-relevant
- การทดสอบด้วยโปรแกรมอ่านหน้าจอ