【发布时间】:2012-12-07 23:26:59
【问题描述】:
我正在为学校做这个作业,但我找不到某个地方的逻辑。我几乎 100% 确定它是 setElemId() 函数,它应该设置并返回元素 id,但它一直没有返回任何内容,当我为快速链接设置 href 时,它只是设置了 # 而不是ID。
HTML
<body>
<div id="page">
<div id="logo"><img src="hlogo.jpg" alt="Historic Documents" /></div>
<div id="logosub">Department of History<br />Midwest University</div>
<div id="doc">
<h1 id="doctitle">The Federalist Papers <br />No. 10</h1>
<p id="docsubtitle">The Union as a Safeguard Against
Domestic Faction and Insurrection<br />From the New York Packet. Friday,
November 23, 1787.</p>
<p id="intro">To the people of the state of New York:</p>
<p id="firstp">Among the numerous advantages promised by a well-constructed
Union, none deserves to be more accurately developed than its
tendency to break and control the violence of faction. The friend of
distresses under which we labor have been erroneously charged on the
</p>
<p>By a <dfn id="firstkey">faction</dfn>, I understand a number of
citizens, whether amounting to a majority or a minority of the whole,
interest, adversed to the rights of other citizens, or to the permanent
and aggregate interests of the community.</p>
<p>The other point of difference is, the greater number of citizens and
extent of territory which may be brought within the compass of
the former than in the latter. The smaller the society, the fewer
</p>
</div>
</div>
</body>
</html>
Javascript
function addEvent(object, evName, fnName, cap) {
if (object.attachEvent)
object.attachEvent("on" + evName, fnName);
else if (object.addEventListener)
object.addEventListener(evName, fnName, cap);
}
addEvent(window, "load", makeKeyWordBox, false);
// Returns an array of Text
function makeElemList(elem)
{
var elemList = document.getElementsByTagName('dfn');
var elemTextArr = new Array();
for(var i = 0; i < elemList.length; i++)
elemTextArr[i] = elemList[i].innerText.toLowerCase();
elemTextArr.sort();
return elemTextArr;
}
// Searches for and returns a single ID
function setElemId(elem, elemText)
{
var elemList = document.getElementsByTagName(elem);
var elemTextArr = new Array();
var elemId;
for(var i = 0; i < elemList.length; i++)
{
elemTextArr[i] = elemList[i].innerText.toLowerCase();
if(elemTextArr[i] == elemText)
{
if(elemList[i].id = null)
{
elemId = elemText + i;
elemList[i].setAttribute('id', elemId);
return elemId;
}
else
{
alert(elemList[i].id);
elemId = elemList[i].id;
return elemId;
}
}
}
}
function makeKeyWordBox()
{
var keywords = makeElemList('dfn');
var historyDoc = document.getElementById('doc');
var keywordBoxTitle = document.createElement('h1');
keywordBoxTitle.innerText = "Keywords";
var keywordBox = document.createElement('div');
keywordBox.id = "keywords";
keywordBox.appendChild(keywordBoxTitle);
var ulList = document.createElement('ul');
keywordBox.appendChild(ulList);
for(var i = 0; i < keywords.length; i++)
{
var linkId = setElemId('dfn', keywords[i]);
var newListItem = document.createElement('li');
var newLink = document.createElement('a');
newLink.innerText = keywords[i];
newLink.href = "#" + linkId;
newListItem.appendChild(newLink);
ulList.appendChild(newListItem);
}
historyDoc.insertBefore(keywordBox, historyDoc.firstChild);
}
【问题讨论】:
-
我一直在检查并提醒函数的不同部分,以找出哪里出了问题,它会遇到 if 语句,但它要么 a) 总是错误的(就像它有一个ID 已设置)或 b)只是不创建 ID。我已经用我当前遇到的问题更新了我的问题。
标签: javascript html search keyword