aria-live
is your friend here.
Simply add a visually hidden div to the page with the aria-live="polite"
attribute.
Then when you add a new row change the text to "new row [rowNumber] added to bottom of the previous table" or similar.
The reason we need to insert the rowNumber is because aria-live
only announces if the text inside it changes. It also has the added minor benefit that it lets someone using a screen reader who wants to add 10 rows know what row number they are on so that they can keep track without having to keep going back to the table.
Obviously if this isn't a table then make sure there is some way of identifying the number of "rows" added such as using an ordered or unordered list.
var btn = document.querySelector("button");
var announcer = document.querySelector("div");
btn.addEventListener("click", addRow);
var rowNumber = 1;
function addRow(){
rowNumber++;
announcer.innerHTML = "new row " + rowNumber + " added to bottom of previous table";
}
.visually-hidden {
border: 0;
padding: 0;
margin: 0;
position: absolute !important;
height: 1px;
width: 1px;
overflow: hidden;
clip: rect(1px 1px 1px 1px); /* IE6, IE7 - a 0 height clip, off to the bottom right of the visible 1px box */
clip: rect(1px, 1px, 1px, 1px); /*maybe deprecated but we need to support legacy browsers */
clip-path: inset(50%); /*modern browsers, clip-path works inwards from each corner*/
white-space: nowrap; /* added line to stop words getting smushed together (as they go onto seperate lines and some screen readers do not understand line feeds as a space */
}
<button class="add-new-row">Add Row</button>
<div class="visually-hidden" aria-live="polite"></div>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…