<!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> <h2> What is the Document object Model?</h2> <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 >source</a></sup>
elem.appendChild(superscript); //追加為變量elem的最后一個子節點
}
}
addLoadEvent(displayCitations);瀏覽器效果:

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。