您的位置:首页 > Web前端 > JavaScript

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

2015-11-18 17:07 1021 查看
将文档中的<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.

W3Cworld wide webconsortiumDOMDocument Object ModelAPIApplication Programming InterFace
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  javascript