关闭

JavaScript DOM编程艺术(第二版) 第八章最终代码

43人阅读 评论(0) 收藏 举报

html代码:

<html>
<head>
    <meta charset = "utf-8">
    <title>Explaning the DOM</title>
	<style type="text/css">
		abbr{
			text-decoration:none;
		}
	</style>
</head>
<body>
	<ul id = "navigation">
		<li><a href ="index.html" accesskey="1">Home</a></li>
		<li><a href ="search.html" accesskey="4">search</a></li>
		<li><a href ="contact.html" accesskey="9">Contact</a></li>
	</ul>
    <h1>What is the Document Object Model?</h1>
    <p>The <abbr title="World Wide Web Consortium">W3C</abbr> defines the <abbr title="Document 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 style of documents.
        </p>
    </blockquote>
    <p>
        It is an <abbr title="Application Programing Interface">API</abbr> that can be used to navigate <abbr title="Hyper Text Markup Language">HTML</abbr> and <abbr title="eXtensible Markup Language">XML</abbr>documents.
    </p>
    <script src="scripts/addLoadEvent.js"></script>
    <script src="scripts/displayAbbreviations.js"></script>
    <script src="scripts/displayCitations.js"></script>
	<script src="scripts/displayAccesskeys.js"></script>
</body>
</html>

Jsp函数:displayCitations.js

function displayAbbreviations(){
	if(!document.getElementsByTagName) return false;
	if(!document.createElement) return false;
	if(!document.createTextNode) return false;
	var abbreviations = document.getElementsByTagName("abbr");
	if(abbreviations.length < 1) return false;
	var defs = new Array();
	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;
	}
	var dlist = document.createElement("dl");
	for(key in defs){
		var definition = defs[key];
		var dtitle = document.createElement("dt");
		var dtitle_text = document.createTextNode(key);
		dtitle.appendChild(dtitle_text);
		var ddesc = document.createElement("dd");
		var ddesc_text = document.createTextNode(definition);
		ddesc.appendChild(ddesc_text);
		dlist.appendChild(dtitle);
		dlist.appendChild(ddesc);
	}
	if(dlist.childNodes.length < 1) return false;
	var header = document.createElement("h2");
	var head_text = document.createTextNode("Abbreviations");
	header.appendChild(head_text);
	document.body.appendChild(header);
	document.body.appendChild(dlist);
}
function displayCitations(){
	if(!document.getElementsByTagName) return false;
	if(!document.createElement) return false;
	if(!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");
		var quoteChildren = document.getElementsByTagName("*");
		if(quoteChildren.length < 1){
			continue;
		}
		var elem = quoteChildren[quoteChildren.length-1];
		var _link = document.createElement("a");
		var link_text = document.createTextNode("source");
		_link.setAttribute("href",url);
		_link.appendChild(link_text);
		var supscript = document.createElement("sup");
		supscript.appendChild(_link);
		elem.appendChild(supscript);
	}
}

addLoadEvent(displayAbbreviations);
Jsp函数:displayCitations.js

function displayCitations(){
	
	if(!document.getElementsByTagName) return false;
	if(!document.createElement) return false;
	if(!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");
		var quoteChildren = quotes[i].getElementsByTagName("*");
		if(quoteChildren.length < 1){
			continue;
		}
		var elem = quoteChildren[quoteChildren.length-1];
		
		var _link = document.createElement("a");
		var link_text = document.createTextNode("source");
		_link.setAttribute("href",url);
		_link.appendChild(link_text);
		var supscript = document.createElement("sup");
		supscript.appendChild(_link);
		elem.appendChild(supscript);
	}
}
addLoadEvent(displayCitations);

Jsp函数:displayAccesskeys.js

function displayAccesskeys(){
	
	if(!document.getElementsByTagName) return false;
	if(!document.createElement) return false;
	if(!document.createTextNode) return false;
	var links = document.getElementsByTagName("a");
	var akeys = new Array();
	for(var i=0;i<links.length;i++){
		var current_link = links[i];
		if(!current_link.getAttribute("accesskey")) continue;
		var key = current_link.getAttribute("accesskey");
		var tex = current_link.lastChild.nodeValue;
		akeys[key] = tex;
	}
	var list = document.createElement("ul");
	for(key in akeys){
		var tex = akeys[key];
		var str = key + ": " + tex;
		var item = document.createElement("li");
		var item_text = document.createTextNode(str);
		item.appendChild(item_text);
		list.appendChild(item);
	}
	var header = document.createElement("h3");
	var header_text = document.createTextNode("Accesskeys");
	header.appendChild(header_text);
	document.body.appendChild(header);
	document.body.appendChild(list);
}
addLoadEvent(displayAccesskeys);
Jsp函数:addLoadEvent.js

function addLoadEvent(func){
	var oldonload = window.onload;
	if(typeof window.onload != 'function'){
		window.onload = func;
	}else{
		window.onload = function(){
			oldonload();
			func();
		}
	}
}



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:149次
    • 积分:9
    • 等级:
    • 排名:千里之外
    • 原创:0篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档