代码之家  ›  专栏  ›  技术社区  ›  martini449

NVDA忽略具有role=“alert”的角度*ngIf块

  •  4
  • martini449  · 技术社区  · 8 年前

    我的目标是向用户提供有关输入错误的信息。在这个例子中,我可以在没有角度的情况下做到这一点。我还设法使用“innerHTML”属性重新创建了下面的示例,但它也不适用于NVDA,简而言之,当发生错误时,它只是将一个节点附加到正文中

    <!DOCTYPE html>
    <html><head>
    <meta http-equiv="content-type" content="text/html; charset=windows-1252">
    <title>Contact form (WAI-ARIA)</title>
    <script type="text/javascript">
      function removeOldAlert()
      {
        var oldAlert = document.getElementById("alert");
        if (oldAlert)
          document.getElementById("error").removeChild(oldAlert);
      }
    
      function addAlert(aMsg)
      {
        removeOldAlert();
        var newAlert = document.createElement("div");
        newAlert.setAttribute("role", "alert");
        newAlert.setAttribute("id", "alert");
        var msg = document.createTextNode(aMsg);
        newAlert.appendChild(msg);
        document.getElementById("error").appendChild(newAlert);
      }
    
      function checkEntryValidity(aID, aSearchTerm, aMsg)
      {
        var elem = document.getElementById(aID);
        var invalid = (elem.value.indexOf(aSearchTerm) < 0);
        if (invalid) {
          elem.setAttribute("aria-invalid", "true");
          addAlert(aMsg);
        } else {
          elem.setAttribute("aria-invalid", "false");
          removeOldAlert();
        }
      }
    </script>
    </head>
    <body>
    <form method="post" action="post.php">
    <fieldset><legend>Please enter your contact details</legend>
    <label for="name">Your name (required):</label>
    <input name="name" id="name" aria-required="true" onblur="checkEntryValidity('name', ' ', 'Invalid name entered!');" aria-invalid="true" value=""><br>
    <label for="email">E-Mail address (required):</label>
    <input name="email" id="email" aria-required="true" onblur="checkEntryValidity('email', '@', 'Invalid e-mail address');" aria-invalid="true" value=""><br>
    <label for="website">Website (optional):</label>
    <input name="website" id="website" value="">
    </fieldset>
    <label for="message">Please enter your message (required):</label><br>
    <textarea name="message" id="message" rows="5" cols="80" aria-required="true"></textarea><br>
    <input name="submit" value="Send message" type="submit">
    <input name="reset" value="Reset form" type="reset">
    </form>
    <div id="error"></div>
    
    </body></html>
    

    screenshot with working example (警报:输入的名称无效!)

    但是NVDA忽略了这种类型的警报消息

    <!-- widget with model binding here -->    
    <div role="alert" class="validation" *ngIf="model.hasErrors()">
            <div *ngFor="let message of model.errors">{{message}}</div>
        </div>
    

    screenshot with failure

    我不知道NVDA为什么不注册属性更改。

    角度2

    我使用的是Windows 10、Firefox 50.1.0和NVDA 2016.4。

    1 回复  |  直到 8 年前
        1
  •  6
  •   martini449    8 年前

    似乎添加aria就是这样生活的

       <div aria-live="assertive">
        <div role="alert" class="validation" *ngIf="model.hasErrors()">
            <div *ngFor="let message of model.errors">{{message}}</div>
        </div>
       </div>
    

    允许读取器注册更改。

    推荐文章