码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2016-05-26 22:13:57      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:js

<!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);


浏览器效果:

技术分享

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

标签:js

原文地址:http://xuyran.blog.51cto.com/11641754/1783621

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!