本文示範如何讀取 iframe 的 longdesc 屬性。
longdesc 屬性指向一個包含詳細描述資訊的頁面網址,主要用途是提供無障礙輔助說明。
<!DOCTYPE html> <html> <body> <iframe id="myframe" src="/default.asp" longdesc="jb51.net"> <p>Your browser does not support iframes.</p> </iframe> <p>The URL of the long description is: <script> document.write(document.getElementById("myframe").longDesc); </script> <p> </body> </html>
上述程式碼的輸出結果為:
The URL of the long description is:jb51.net
補充說明:JavaScript 中的屬性名同樣是駝峰寫法 longDesc。不過 longdesc 在 HTML5 中已被標示為不建議使用,目前較推薦的做法是改用 aria-describedby 指向描述內容的 id,或直接在 iframe 旁以可見文字說明。
希望本文所述對大家的 JavaScript 程式設計有所幫助。