[toc]javascript
不要使用dom技術把一些重要的東西加到網頁上,你可能正在濫用dom 咱們要始終牢記兩個原則css
咱們能夠用css把本來縱向排列的元素顯示在一行,設置display屬性爲inlinehtml
不一樣的瀏覽器呈現例外的屬性千姿百態,有些瀏覽器會把title屬性顯示爲彈出式的提示框,另外一些瀏覽器會把它顯示在狀態欄裏。java
有些瀏覽器把alt設置爲彈出式的提示框,alt屬性的本來用途:在圖片沒法顯示時,用一段文字來描述圖片node
本章內容:瀏覽器
<abbr>
標籤是縮略語 <acronym>
標籤是首字母縮寫詞 dom念成一個單詞就是首字母縮寫詞,念成三個字母就是縮略語app
對於標記語言選擇HTML仍是XHTML是你的自由,可是使用的標記必須和你選用的 DOCTYPE聲明保持一致dom
建議使用XHTML,語言有求嚴格函數
HTML5文檔類型聲明簡單,才15個字符oop
略
縮略語標籤title屬性是隱藏的,咱們能夠用dom改變瀏覽器的默認行爲
把<abbr>
標籤的title屬性集中顯示在一個界面,用一個定義列表元素顯示標籤包含的文本和title屬性 定義列表以下
<dl>javascript <dt>W3C</dt> <dd>描述</dd> <dt>DOM</dt> <dd>描述</dd> <dt>API</dt> <dd>描述</dd> <dt>HTML</dt> <dd>描述</dd>
能夠用DOM建立這個元素
functin displayAbbreviations(){ var abbreviations = document.getElementByTagName("abbr"); if (abbreviations.length < 1) return false; var defs = new Array(); for (var i = 0; i < abbreviattions.length; i++){ var current_abbr = abbreviations[i]; var definition = curent_abbr.getAttribute("title"); bar key = current_abbr.lastChilde.nodeValue; def[key] = definition; } }
把循環體寫成一條語句,比較難讀
for (var i = 0; i < abbreviattions.length; i++){ defs[abbreviations[i].lastChilde.nodeValue] = abbreviations[i].getAttribute("title"); }
<dl> <dt>Title 1</dt> <dd>Description 1</dd> <dt>Title 2</dt> <dd>>Description 2</dd>
建立定義列表
for (key in defs){ var definition = def[key]; var dtitle = document.creatElement("dt"); var dtitle_text = document.creatTextNode(key); dtitle.appendChild(dtitle_text); var ddesc = document.creatElement("dd"); var ddesc_text = document.creatTextNode(definition); ddesc.appendchild(ddesc_text); dlidt.appendChild(dtitle); dlidt.appendChild(ddesc); }
document.getElementByTagName("body")[0]
第二種:使用HTML-DOM 插入縮略語表標題:document.body.appendChild(header); 插入縮略語表自己: document.body.appendChild(dlist);function displayAbbreviations() { if (!document.getElementsByTagName || !document.createElement || !document.createTextNode) return false; // get all the abbreviations var abbreviations = document.getElementsByTagName("abbr"); if (abbreviations.length < 1) return false; var defs = new Array(); // loop through the abbreviations for (var i=0; i<abbreviations.length; i++) { var current_abbr = abbreviations[i]; //if (current_abbr.childNodes.length < 1) continue; var definition = current_abbr.getAttribute("title"); var key = current_abbr.lastChild.nodeValue; defs[key] = definition; } // create the definition list var dlist = document.createElement("dl"); // loop through the definitions for (key in defs) { var definition = defs[key]; // create the definition title var dtitle = document.createElement("dt"); var dtitle_text = document.createTextNode(key); dtitle.appendChild(dtitle_text); // create the definition description var ddesc = document.createElement("dd"); var ddesc_text = document.createTextNode(definition); ddesc.appendChild(ddesc_text); // add them to the definition list dlist.appendChild(dtitle); dlist.appendChild(ddesc); } //if (dlist.childNodes.length < 1) return false; // create a headline var header = document.createElement("h2"); var header_text = document.createTextNode("Abbreviations"); header.appendChild(header_text); // add the headline to the body document.body.appendChild(header); // add the definition list to the body document.body.appendChild(dlist); } addLoadEvent(displayAbbreviations);
addLoadEvent函數
function addLoadEvent(func) { var oldonload = window.onload; if (typeof window.onload != 'function') { window.onload = func; } else { window.onload = function() { oldonload(); func(); } } }
若是把displayAbbreviation腳本在IE6,或者更早的window版本打開,則javascript出錯 在網景公司與微軟的瀏覽器大戰中,微軟決定不在本身的瀏覽器實現abbr元素,這就是地雷 解決方法:保證該函數能在IE中平穩退化
function displayAbbreviations() { if (!document.getElementsByTagName || !document.createElement || !document.createTextNode) return false; // get all the abbreviations var abbreviations = document.getElementsByTagName("abbr"); if (abbreviations.length < 1) return false; var defs = new Array(); // loop through the abbreviations for (var i=0; i<abbreviations.length; i++) { var current_abbr = abbreviations[i]; **if (current_abbr.childNodes.length < 1) continue;** var definition = current_abbr.getAttribute("title"); var key = current_abbr.lastChild.nodeValue; defs[key] = definition; } // create the definition list var dlist = document.createElement("dl"); // loop through the definitions for (key in defs) { var definition = defs[key]; // create the definition title var dtitle = document.createElement("dt"); var dtitle_text = document.createTextNode(key); dtitle.appendChild(dtitle_text); // create the definition description var ddesc = document.createElement("dd"); var ddesc_text = document.createTextNode(definition); ddesc.appendChild(ddesc_text); // add them to the definition list dlist.appendChild(dtitle); dlist.appendChild(ddesc); } **if (dlist.childNodes.length < 1) return false;** // create a headline var header = document.createElement("h2"); var header_text = document.createTextNode("Abbreviations"); header.appendChild(header_text); // add the headline to the body document.body.appendChild(header); // add the definition list to the body document.body.appendChild(dlist); } addLoadEvent(displayAbbreviations);
第一條語句:若是當前元素沒有子節點,就馬上開始下一循環 第二條語句:若是對應縮略語dl元素沒有子節點,馬上退出displayAbbreviations函數
blockquote元素包含一個cite屬性,可選屬性,能夠給他一個url地址。把文獻資料和相關網頁連接起來的作好方法。 在實踐中,瀏覽器會忽視這個屬性,用戶沒法看到,咱們能夠利用javascript和dom把收集,顯示在網頁上
編寫displayCitations函數
function displayCitations() { if (!document.getElementsByTagName || !document.createElement || !document.createTextNode) return false; // get all the blockquotes var quotes = document.getElementsByTagName("blockquote"); // loop through all the blockquotes for (var i=0; i<quotes.length; i++) { // if there is no cite attribute, continue the loop if (!quotes[i].hasAttribute("cite")) continue; // store the cite attribute var url = quotes[i].getAttribute("cite"); // get all the element nodes in the blockquote var quoteChildren = quotes[i].getElementsByTagName('*'); // if there are no element nodes, continue the loop if (quoteChildren.length < 1) continue; // get the last element node in the blockquote var elem = quoteChildren[quoteChildren.length - 1]; // create the markup var link = document.createElement("a"); var link_text = document.createTextNode("source"); link.appendChild(link_text); link.setAttribute("href",url); var superscript = document.createElement("sup"); superscript.appendChild(link); // add the markup to the last element node in the blockquote elem.appendChild(superscript); } } addLoadEvent(displayCitations);
編寫一個函數把文檔中全部用到的快捷鍵顯示在頁面裏 accesskey屬性能夠把一個元素與鍵盤上特定按鍵關聯在一塊兒 一些基本快捷鍵約定成俗的設置方法
利用DOM動態建立快捷鍵清單
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);
略