[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);
略