JS DOM:显示文献来源链接表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<!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>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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");
forvar 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);


浏览器效果:

JS DOM:显示文献来源链接表

本文转自  小旭依然  51CTO博客,原文链接:http://blog.51cto.com/xuyran/1783621

上一篇:C++中如何在一个构造函数中调用另一个构造函数


下一篇:NetApp创始人谈闪存、SAS和云存储