JavaScript DOM编程艺术—显示“缩略语列表”

将文档中的<abbr>标签中的title属性集中起来显示在一个页面.

test.html

<!document html>
<html lang="en">
<head>
	<meta charset="utf-8" />
	<title>Explaining the Document Object Model</title>
</head>
<body>	
	<h1>what is the Document Object Model?</h1>
<p>
	The <abbr title="world wide webconsortium">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. structures and style of documents.
</p>
</blockquote>
<p>
it is an <abbr title="Application Programming InterFace">API</abbr> that can be used to navigate HTML and XML documents.
</p>
<script type="text/javascript" src="addLoadEvent.js"></script>
<script type="text/javascript" src="displayAbbr.js"></script>
</body>
</html>

addLoadEvent.js

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

displayAbbr.js

addLoadEvent(displayAbbr);
function displayAbbr(){
	if(!document.getElementById) return false;
	if(!document.getElementsByTagName) return false;
	if(!document.createElement) return false;
	 var abbrs = document.getElementsByTagName("abbr");
	// if(abbrs.length < 1) return false;
	 var arr = new Array();
	for(var i=0; i<abbrs.length; i++){
		var abbr = abbrs[i];
		if(abbr.childNodes.length < 1) continue;
		var key = abbr.lastChild.nodeValue;
		var description = abbr.getAttribute("title");
		arr[key] = description;
	}
	//创建定义列表
	var xdl = document.createElement("dl");
	for(key in arr){
		var xdt = document.createElement("dt");
		var atxt = document.createTextNode(key);
		xdt.appendChild(atxt);
		var xdd = document.createElement("dd");
		var btxt = document.createTextNode(arr[key]);
		xdd.appendChild(btxt);
		xdl.appendChild(xdt);
		xdl.appendChild(xdd);
	}
	 document.body.appendChild(xdl);
}
显示效果:

what is the Document Object Model?

The W3C defines the DOM as:

A platform and language-neutral interface that will allow programs and scripts to dynamically access and update the content. structures and style of documents.

it is an API that can be used to navigate HTML and XML documents.

W3C
world wide webconsortium
DOM
Document Object Model
API
Application Programming InterFace


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值