How to notify screen readers using WAI-ARIA that a div is now visible

前端 未结 4 1707
隐瞒了意图╮
隐瞒了意图╮ 2020-12-13 20:47

How do you notify screen readers using WAI-ARIA that a div is now visible?

If we got the html

Present main content
4条回答
  •  失恋的感觉
    2020-12-13 21:23

    I created a sample showing how you could using role="alert" to notify screen reader users when they are approaching the character limit of a textarea element, if you are trying to understand how to use the alert role, it may help:

    There's more to it, but here's the small part of the code which produces the alert:

    if (characterCount > myAlertTheshold) {
           var newAlert = document.createElement("div"); /* using the native js because it's faster */
           newAlert.setAttribute("role", "alert");
           newAlert.setAttribute("id", "alert");
           newAlert.setAttribute("class","sr-only");
           var msg = document.createTextNode('You have ' + charactersRemaining +' characters of ' + myMaxLength + ' left');
           newAlert.appendChild(msg);
           document.body.appendChild(newAlert);
         }
    

    http://codepen.io/chris-hore/pen/emXovR

提交回复
热议问题