2012-12-07 72 views
0

我正在为这所学校做这项任务,但是我找不到一个逻辑。我几乎100%肯定它是setElemId()函数,它应该设置并返回元素ID,但它不会返回任何内容,并且当我为快速链接设置href时,它只设置#而不是ID通过JavaScript设置ID(KeywordScript)

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); 
} 
+0

我一直在经历并警告函数的不同部分,以找出它出错的地方,它会打if语句,但它会a)总是假的(比如它已经设置了ID)或者b)只是不创建id。我已经更新了我目前遇到的问题。 –

回答

0

“它会击中if语句,但它要么一)始终是假的(就像它已经设置一个ID)”

尝试改变:

if(elemList[i].id = null) 

到:

if(!elemList[i].id) 

您正在分配idnull而不是将其比较null(您使用=当你的意思是=====),然后if将每一次评估,作为null(falsy),并进入else情况。

此外,我不认为你要比较null无论如何,因为没有设置id不会null(当我试图解封id是一个空字符串,"")。更容易使用!运营商来测试id是否“虚假”。

0

你似乎是搜索的关键字精确匹配。由于你的元素没有包含你正在寻找的关键字(而是它们包含文本),所以没有匹配。

相反,尝试if(elemTextArr[i].indexOf(elemText) > -1)

所以请注意,您似乎没有被转换搜索词条中以小写,所以如果我在一个大写字母I型不会得到任何不匹配。

+0

我将它们转换为小写,因为我在循环中测试它们。仍然是你的代码没有去。虽然谢谢! –