Связь ошибок с полями с помощью aria-describedby
Сделайте так, чтобы сообщение читалось вместе с полем ввода.
«Связь ошибок с полями с помощью aria-describedby» — бесплатный урок Web Accessibility Academy на CoddyKit. Это урок 1 из 4. Ты можешь прочитать весь урок бесплатно ниже — а потом практиковать его прямо в браузере с встроенным редактором кода и ИИ-репетитором 24/7. Это часть пути обучения Web Accessibility Academy, и твой прогресс синхронизируется между веб-версией и приложением CoddyKit. Курс Web Accessibility Academy содержит 4 уроков всего.
Части этого урока еще не переведены и отображаются на английском.
A Floating Error
An error message sitting near a field looks connected to you. But to a screen reader it is just loose text the user may never hear. ⚠️
The Fix Is a Link
You need to formally tie the message to its input. The attribute that does this is aria-describedby, and it lives on the field.
Point to an id
Give the error element an id, then set the input's aria-describedby to that same id. Now the two are officially linked.
<input id="email" aria-describedby="email-err">
<p id="email-err">Enter a valid email.</p>What the User Hears
When focus lands on the input, the screen reader reads the label, the type, and then the description. The error is no longer orphaned.
Name Then Description
aria-describedby does not replace the label. The label gives the name; describedby adds extra info that is read after it.
Multiple Descriptions
You can list several ids, space-separated. The browser reads them in order, so you can join a hint and an error together.
<input aria-describedby="hint err">Only When It Applies
Add the error id to aria-describedby only while the error is showing. Pointing at an empty or hidden node can read nothing or confuse users.
Keep the Text Real
The described element must hold real, visible text. aria-describedby reads its content, so an empty span describes nothing useful.
Hidden Targets Stay Silent
If the target has display none, most screen readers skip it. Keep error text rendered and visible so the description is actually announced.
Works With aria-invalid
Pair describedby with aria-invalid on the same field. One says this failed; the other says here is why. Together they tell the full story.
Not Just for Errors
The same attribute carries format hints too, like password rules. Errors are one common use, not the only one.
<input aria-describedby="pw-rule">
<p id="pw-rule">At least 8 characters.</p>Quick Check
How do you make a screen reader read an error with its field?
Recap
Tie errors to fields with aria-describedby pointing at a real, visible id. The screen reader then reads the field and its error together. ✅
Часто задаваемые вопросы
Урок «Связь ошибок с полями с помощью aria-describedby» бесплатный?
Да — полный текст урока «Связь ошибок с полями с помощью aria-describedby» бесплатно доступен здесь в веб-версии. Чтобы практиковать его интерактивно (встроенный редактор кода и ИИ-репетитор 24/7) и разблокировать остальной курс Web Accessibility Academy, подпишись на CoddyKit PRO. Курс Web Accessibility Academy содержит 4 уроков всего.
Чему я научусь в уроке «Связь ошибок с полями с помощью aria-describedby»?
Сделайте так, чтобы сообщение читалось вместе с полем ввода. Ты практикуешь Web Accessibility Academy с помощью реального кода, который запускаешь прямо в браузере, и ИИ-репетитор 24/7 отвечает на твои вопросы во время урока.
Нужен ли мне опыт, чтобы начать Web Accessibility Academy?
Предыдущий опыт не требуется. Web Accessibility Academy на CoddyKit структурирован для всех уровней — от новичков до продвинутых, поэтому ты можешь начать отсюда или с самого начала и учиться в своем темпе. Это урок 1 из 4.
Сколько времени занимает урок «Связь ошибок с полями с помощью aria-describedby»?
Большинство уроков CoddyKit занимают около 5–10 минут. Каждый из них компактный и интерактивный, поэтому ты постоянно делаешь прогресс и продолжаешь с того же места в веб-версии и приложении.
Можно ли писать и запускать код в этом уроке Web Accessibility Academy?
Да. Каждый урок Web Accessibility Academy включает встроенный редактор кода, поэтому ты пишешь и запускаешь реальный код прямо в браузере и получаешь моментальную обратную связь от AI — локальная установка не требуется.
Все уроки этого курса
- Связь ошибок с полями с помощью aria-describedby
- Обозначение недопустимого состояния с помощью aria-invalid
- Перемещение фокуса к первой ошибке
- В строке, при потере фокуса или при отправке: время имеет значение