Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
179 views
in Technique[技术] by (71.8m points)

html - Having content in a javascript created dialog read by a screen reader

I am using the javascript library vex to output dialogs like this

login failed dialog

When I used this with a screen reader (narrator), that text is not read so I tried to change the javascript inside vex that manipulates the DOM.

The resulting DOM became like this

<div class="vex vex-theme-default" role="dialog">
   <div class="vex-content">
      <form class="vex-dialog-form">
         <div class="vex-dialog-message" tab-index="0"
            aria-label="Login failed">Login failed!</div>
         <div class="vex-dialog-input"></div>
         <div class="vex-dialog-buttons">
            <button type="submit"
              class="vex-dialog-button-primary btn vex-dialog-button vex-first">Ok</button>
         </div>
      </form>
   </div>
</div>

but the message "Login failed" is not read even with this markup.

Why not?

(I have also called .focus() on the div with the class vex-dialog-message).

The screen reader will instead continue to read the text after the button that was pressed when trying to login (that is running an ajax request that tries to login so the document is never reloaded).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is what role="alert" is for.

It will interrupt a screen reader (as it is the equivalent of aria-live="assertive") with any immediately important information.

As this is a dialog you may instead want to look at role="alertdialog" to replace your role="dialog".

Additionally when you focus the alert don't make the "Login Failed!" text tabindex="0".

Instead use tabindex="-1" so that you can still focus it programatically but it does not end up in the focus order of the page.

The pattern you currently use will be tricky (not impossible) to get right as your "OK" (submit) button is within a form, so this will send screen readers into "forms mode", which behaves differently from normal reading mode.

As such if you are able to create a custom component there is a great writeup from deque on how best to implement an alertdialog

An example of the markup used there is as follows, notice the use of aria-describedby and aria-labelledby to maximise the chances of the alert being read correctly:

    <div role="alertdialog" aria-labelledby="alertHeading" aria-describedby="alertText">
        <h1 id="alertHeading">Warning! Street Address Mismatch!</h1>
        <div id="alertText">
            <p>The street address you entered does not match the Postal Code Data.</p>
            <p>Is this the correct format: 12345 Main St. Blvd.?</p>
        </div>
        <button id="yes">Yes, Format Is Correct</button>
        <button>No, Edit Street Address</button>
    </div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...