<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Explaining the Ddocument Ob Model</title> <link href="style08.css" type="text/css" rel="stylesheet" /> </head> <body> <h1> What is the Document object Model?</h1> <p> The <abbr title="Worle Wide Web Consortium">W3C</abbr> defines the <abbr title="Object Model">DOM</abbr> as: </p> <blockquote cite="http://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 styles of documents. </p> </blockquote> <p> It is an <abbr title="Application Programming Interface">API</abbr> that can be used to navigate <abbr title="eXtensible Markup Language">XML</abbr> documents. </p> <script src="8.5.js"></script> </body> </html>
function addLoadEvent(func){ //不管在页面加载完毕执行多少个函数,都应付自如
var oldonload = window.onload;
if(typeof window.onload != ‘function‘){
window.onload = func;
}else{
window.onload = function(){
oldonload();
func();
}
}
}
function displayCitations(){
//检查兼容性
if(!document.getElementsByTagName||!document.createElement||!document.createTextNode) return false;
var quotes = document.getElementsByTagName("blockquote");
for( var i = 0;i < quotes.length; i++){
if(!quotes[i].getAttribute("cite")) continue;
var url = quotes[i].getAttribute("cite"); //获取cite属性的值:也就是链接
var quoteChildern = quotes[i].getElementsByTagName("*"); //因为p节点和blockquote节点之间有个换行符,不少浏览器会把它解释为几个文本节点,这样一来,blockquote元素节点的lastChild属性
//检查长度是否小于1 ,如果是,立刻退出本次循环。 //就是一个文本节点不是p元素节点。该条语句是为了获取最后一个元素节点的位置。建议:如果没有百分之百把握,一定要去检查
if(quoteChildern.length < 1) continue; //lastChild的nodeType属性,以免获取节点错误。
var elem = quoteChildern[quoteChildern.length - 1];
var link = document.createElement("a"); //创建a元素
var link_text = document.createTextNode("source");
link.appendChild(link_text);
link.setAttribute("href", url); //把href属性添加给新链接
//插入链接
var superscript = document.createElement("sup"); //sup元素:指定内含文本要以上标的形式显示,通常比当前字体稍小。
superscript.appendChild(link); //以上语句html文本:<sup><a href="http://www.w3.org/DOM/">source</a></sup>
elem.appendChild(superscript); //追加为变量elem的最后一个子节点
}
}
addLoadEvent(displayCitations);浏览器效果:
原文:http://xuyran.blog.51cto.com/11641754/1783621