本文示範如何透過圖片的 alt 屬性取得替代文字。
alt 在圖片無法載入時作為替代顯示,同時也是螢幕閱讀器朗讀的依據,屬於基本的無障礙要求。
<!DOCTYPE html> <html> <body> <img id="compman" src="compman.gif" alt="Crazy computerman" width="107" height="98"> <p>The value of the alt atrribute is: <script> document.write(document.getElementById("compman").alt); </script> </p> </body> </html>
小提醒:裝飾性的圖片雖然要有 alt 屬性,但值應留空(alt=""),這樣螢幕閱讀器才會跳過它;若完全省略 alt,部分輔具反而會改去朗讀檔名,體驗更差。
希望本文所述對大家的 JavaScript 程式設計有所幫助。