<!DOCTYPE?html>
<html>
<head>
<title>縮略語列表</title>
<link?rel="stylesheet"?href="tst.css"?type="text/css"?/>
</head>
<body>
<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.w3c.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?Programming?Interface">API</abbr>
that?can?be?used?to?navigate?<abbr?title="eXtensible?Markup?Language">XML</abbr>?document.
</p>
<abc></abc>
<script?src="tes.js"></script>
</body>
</html>function?adonload(func){
var?oldonload?=?window.onload;
if(typeof?oldonload?!=?'function'){
window.onload?=?func;
}else{
window.onload?=?function(){
oldonload();
func();
}
}
}
function?insertAfter(newele,tagele){
var?parent?=?tagele.parentNode;
if(parent.lastChild?==?tagele){
parent.appendChild(newele);
}else{
insertBefore(newele,tagele.nextSibling);
}
}
//以上為通用函數(shù)
function?displayAbbreviations(){
if(!document.getElementsByTagName)return?false;
if(!document.createElement)return?false;
if(!document.createTextNode)return?false;
var?abbrEle?=?document.getElementsByTagName("abbr");
if(abbrEle.length?<?1)?return?false;
var?defs?=?new?Array();
for(var?i?=?0;?i<abbrEle.length;?i++){
if(abbrEle[i].childNodes.length?<?1)?continue;??
var?key?=?abbrEle[i].lastChild.nodeValue;
var?describe?=?abbrEle[i].getAttribute("title");
defs[key]?=?describe;
//?alert(defs.length);??這里為什么不能顯示defs的長度???
}
var?dlEle?=?document.createElement("dl");
for(key?in?defs){
var?describe?=?defs[key];
var?dtEle?=?document.createElement("dt");
var?dtEleTxt?=?document.createTextNode(key);
dtEle.appendChild(dtEleTxt);
var?ddEle?=?document.createElement("dd");
var?ddEleTxt?=?document.createTextNode(describe);
ddEle.appendChild(ddEleTxt);
dlEle.appendChild(dtEle);
dlEle.appendChild(ddEle);
}
if(dlEle.childNodes.length?<?1)return?false;
document.body.appendChild(dlEle);
//?alert(defs.length)???這里為什么也不能顯示defs的長度???
var?header?=?document.createElement("h2");
var?headerTxt?=?document.createTextNode("縮略語列表");
header.appendChild(headerTxt);
dlEle.parentNode.insertBefore(header,dlEle);
}
function?displayCite(){
if(!document.getElementsByTagName)return?false;
if(!document.createElement?||?!document.createTextNode)return?false;
var?quote?=?document.getElementsByTagName("blockquote");
for(var?i?=?0;?i<quote.length;?i++){
if(!quote[i].getAttribute("cite"))continue;
var?cite?=?quote[i].getAttribute("cite");
var?links?=?document.createElement("a");
var?linksTxt?=?document.createTextNode("source"+(i+1));
links.appendChild(linksTxt);
links.setAttribute("href",cite[i]);
document.body.appendChild(links);
}
}
adonload(displayAbbreviations);
adonload(displayCite);我要用循環(huán)給數(shù)組添加,每個key對應一個describe,但是我循環(huán)完后想看看數(shù)組添加了多少,理論上應該有4項,但是彈出窗口顯示0
用for循環(huán)給數(shù)組添加元素,為什么用alert顯示還是長度0
開心的山羊
2017-10-13 02:12:04