HTML 中有幾個屬性寫了卻看不見:abbr 的 title、blockquote 的 cite、a 的 accesskey。它們語意上很有價值,但瀏覽器預設幾乎不呈現。本文示範如何用 JavaScript 把它們「挖」出來,變成使用者真的看得到的內容。
出發點是一段含大量縮略語的文字:
<p> The <abbr title="World Wide Web Consortium">W3C</abbr> defines the <abbr title="Document Object Model">DOM</abbr> as: </p> <blockquote cite="https://www.w3.org/DOM/"> <p> A platform- and language-neutral interface that will allow programs and scripts to dynamically access and update the content, structure and style of documents. </p> </blockquote> <p> It is an <abbr title="Application Programming Interface">API</abbr> that can be used to navigate <abbr title="HyperText Markup Language">HTML</abbr> and <abbr title="eXtensible Markup Language">XML</abbr> documents. </p>
abbr 的 title 在瀏覽器裡是隱藏的,而且各瀏覽器對縮略語的預設樣式並不一致,多少會影響體驗。比較好的解法是把這些縮略語用定義列表集中呈現:
<dl> <dt>縮略語標題/abbr.lastChild.nodeValue</dt> <dd>縮略語定義描述/abbr.getAttribute</dd> ...... </dl>
用 DOM 動態建立這個列表,大致流程是:
(1)遍歷所有 abbr;(2)保存每個 abbr 的 title 屬性與文字內容;(3)建立 dl;(4)建立 dt;(5)把 abbr 文字放進 dt;(6)建立 dd;(7)把 title 放進 dd;(8)依序追加各節點。
function displayAbbreviations() { //注釋1:注意這裡沒有對DOM方法做兼容性檢查 var abbreviations = document.getElementsByTagName("abbr"); var defs = new Array();//注釋2:用數組的鍵值對來保存abbr的title屬性和文本 //loop through the abbr list for (var i=0; i<abbreviations.length; i++) { var current_abbr = abbreviations[i]; //注釋3:if (current_abbr.childNodes.length < 1) continue; var defination = current_abbr.getAttribute("title"); var key = current_abbr.lastChild.nodeValue; defs[key] = defination; } var dlist = document.createElement("dl"); //loop through the defs for (key in defs) { var defination = defs[key]; var dtitle = document.createElement("dt"); var dtitle_text = document.createTextNode(key); dtitle.appendChild(dtitle_text); var ddesc = document.createElement("dd"); var ddesc_text = document.createTextNode(defination); ddesc.appendChild(ddesc_text); dlist.appendChild(dtitle); dlist.appendChild(ddesc); } //注釋4:if (dlist.childNodes.length < 1) return false; var header = document.createElement("h2"); var header_text = document.createTextNode("Abbreviations"); header.appendChild(header_text); //注釋5:下面兩行用到了HTML-DOM屬性:document.body,也可以用DOM core的 document.getElementsByTagName("body")[0]方法; document.body.appendChild(header); document.body.appendChild(dlist); }
這個函式還有改進空間。注釋1 提到少了 DOM 方法的相容性檢查;此外 IE6 及更早版本不認 <abbr> 標籤,會在 getElementsByTagName 時永遠回傳 0 個節點。注釋3 的 childNodes 長度判斷可以避開空節點,注釋5 則改用 document.body 並搭配前面的空檢查,能避免在不支援 abbr 的瀏覽器上直接報錯。
做法與縮略語列表幾乎相同。<blockquote> 有個選用的 cite 屬性,用來標示引用來源,值是一個指向網頁的 URL。這個屬性語意上很有用,能把文獻資料與線上來源串起來,但主流瀏覽器都不支援,使用者看不到。
把它以連結形式顯示在引用區塊末尾:
function displayCitations() { //兼容性檢查 if (!document.getElementsByTagName || !document.createElement || !document.createTextNode) return false; //獲取所有的blockquote元素 var quotes = document.getElementsByTagName("blockquote"); //1 遍歷blockquote元素 for (var i=0; i<quotes.length; i++) { // 檢查是否存在cite屬性 if (!quotes[i].getAttribute("cite")) continue; // 2 提取cite屬性的值 var url = quotes[i].getAttribute("cite"); // 獲取blockquote包含的所有元素節點,注意是元素節點,這樣就把文本節點排除掉了 var quoteChildren = quotes[i].getElementsByTagName("*"); // 判斷元素是否為空 if (quoteChildren.length < 1) continue; var elem = quoteChildren[quoteChildren.length - 1];//獲取最後一個元素節點 var link = document.createElement("a");//3 創建鏈接節點 var link_text = document.createTextNode("source"); link.appendChild(link_text); link.setAttribute("href", url);//4 給鏈接節點的href屬性賦值 var superscript = document.createElement("sup"); superscript.appendChild(link); elem.appendChild(superscript);//5 追加節點到<blockquote>包含節點的末尾 } }
getElementsByTagName("*") 取的是所有後代元素節點,天然排除了純文字節點,因此最後一個元素就是內容的結尾,把上標連結掛上去剛剛好。
accesskey 屬性可以讓 <a> 連結與鍵盤特定按鍵關聯,例如 <a href="index.html" accesskey="1">Home</a>。不過並非所有瀏覽器都支援(例如 Opera 就不支援)。
把下面這段導覽的 accesskey 像縮略語那樣以列表呈現:
<ul id="navigation"> <li><a href="index.html" accesskey="1">Home</a></li> <li><a href="search.html" accesskey="4">Search</a></li> <li><a href="contact.html" accesskey="0">Contact</a></li> </ul>
function displayAccesskeys() { if (!document.getElementsByTagName || !document.createElement || !document.createTextNode) return false; // get all the links in the document var links = document.getElementsByTagName("a"); // create an array to store the accesskeys var akeys = new Array(); // loop through the links for (var i=0; i<links.length; i++) { var current_link = links[i]; // if there is no accesskey attribute, continue the loop if (current_link.getAttribute("accesskey") == null) continue; // get the value of the accesskey var key = current_link.getAttribute("accesskey"); // get the value of the link text var text = current_link.lastChild.nodeValue; // add them to the array akeys[key] = text; } // create the list var list = document.createElement("ul"); // loop through the accesskeys for (key in akeys) { var text = akeys[key]; // create the string to put in the list item var str = key + " : "+text; // create the list item var item = document.createElement("li"); var item_text = document.createTextNode(str); item.appendChild(item_text); // add the list item to the list list.appendChild(item); } // create a headline var header = document.createElement("h3"); var header_text = document.createTextNode("Accesskeys"); header.appendChild(header_text); // add the headline to the body document.body.appendChild(header); // add the list to the body document.body.appendChild(list); } addLoadEvent(displayAccesskeys);
三個函式的骨架一致:先做相容性檢查 → 遍歷目標元素 → 取出隱藏屬性 → 用 createElement / createTextNode 組出列表 → 掛到 body。這正是「漸進增強」的典型手法:內容本身在 HTML 裡已經完整,腳本只是把隱藏資訊暴露出來,腳本失效時頁面也不會壞掉。
最後呈現的畫面如下:

以上就是本文的全部內容,希望對你的學習或工作有所幫助。