首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >生成目录中的错误

生成目录中的错误
EN

Stack Overflow用户
提问于 2016-02-20 18:49:49
回答 1查看 858关注 0票数 0

这是另一项作业,我需要一些帮助来完成。需要发生的是,JavaScript查看头部并创建一个列表。我大部分都完成了。我只是需要一些帮助。例如:我在"b“项中输入了什么ID,正如说明中所说的那样,Set the value of the "headingText" variable to the content of the element with an ID = to the current value of the counter variable。这是否意味着a充当一个变量而不必使用var声明?我们很感激你的帮助。

HTML

代码语言:javascript
复制
<article>
 <h2>United States Bill Of Rights</h2>
 <h3>Table Of Contents</h3>
 <ul>
 <!-- The Table Of Contents will appear here. -->
 </ul>

 <h3 id="1">Amendment I</h3>
 <p>Congress shall...</p>
 <h3 id="2">Amendment II</h3>
 <p>A well regulated Militia...</p>
 <h3 id="3">Amendment III</h3>
 <p>No Soldier shall...</p>
 <h3 id="4">Amendment IV</h3>
 <p>The right of the people...</p>
 <h3 id="5">Amendment V</h3>
 <p>No person shall be held...</p>
 <h3 id="6">Amendment VI</h3>
 <p>In all criminal prosecutions...</p>
 <h3 id="7">Amendment VII</h3>
 <p>In Suits at common law...</p>
 <h3 id="8">Amendment VIII</h3>
 <p>Excessive bail shall...</p>
 <h3 id="9">Amendment IX</h3>
 <p>The enumeration in the Constitution...</p>
 <h3 id="10">Amendment X</h3>
 <p>The powers not delegated to the...</p>
</article>

JavaScript

代码语言:javascript
复制
<script type="text/javascript">
 var TOCEntry = "";

 // References the only "<ul>" in the document.
 var list = document.getElementsByTagName ("ul");

 var headingText= "";

 var TOCEntry = "";

 function createTOC () {
  // a. This counter will = "1", and will repeat the loop as long as the value is LTG "10", and will increment the counter variable by "1" each time through.
  for (a = 1; a <= 10; a++) {

   // Within the "for" loop, add statements that do the following:

   // b. Set the value of the "headingText" variable to the content of the element with an ID = to the current value of the counter variable ("a").
   document.getElementById ("___").innerHTML = headingText;

   // c. Create a new <li> element and assign it as the value of the "TOCEntry" variable.
   var TOCEntry = document.createElement ("li");
   document.body.appendChild (TOCEntry);

   // d. Set the content of the "TOCEntry" Node to the following: "<a href=#" + i ">" + headingText + "</a>".
   var TOCEntry = document.getElementsByTagName ("li").textContent = "";

   // e. Add the "TOCEntry" Node as the last child to the list Node.

  }

 }

 // Backwards-compatibility event listener
 if (window.addEventListener) {
  window.addEventListener ("load", createTOC, false);
 }
 else if (window.attachEvent) {
  window.attachEvent ("onload", createTOC);
 }
</script>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-20 19:40:14

对我为什么这么做的解释将是最底层的

代码语言:javascript
复制
// References the only "<ul>" in the document.
var list = document.getElementById("tableOfContents");
function createTOC () {
  // a. This counter will = "1", and will repeat the loop as long as the value is LTG "10", and will increment the counter variable by "1" each time through.
  for (var a = 1; a <= 10; a++) {
    (function(a) {
    // Within the "for" loop, add statements that do the following:
    // b. Set the value of the "headingText" variable to the content of the element with an ID = to the current value of the counter variable ("a").
    var headingText = document.getElementById(a).innerHTML;
   // c. Create a new <li> element and assign it as the value of the "TOCEntry" variable.
    var TOCEntry = document.createElement ("li");

   // d. Set the content of the "TOCEntry" Node to the following: "<a href=#" + i ">" + headingText + "</a>".
    TOCEntry.innerHTML = "<a href=\"#" + a + "\">" + headingText + "</a>";
    // e. Add the "TOCEntry" Node as the last child to the list Node.
    list.appendChild (TOCEntry);
  })(a);
 }
}
// Backwards-compatibility event listener
if (window.addEventListener) {
 window.addEventListener ("load", createTOC, false);
}
else if (window.attachEvent) {
 window.attachEvent ("onload", createTOC);
}
代码语言:javascript
复制
<article>
 <h2>United States Bill Of Rights</h2>
 <h3>Table Of Contents</h3>
 <ul id="tableOfContents">
 <!-- The Table Of Contents will appear here. -->
 </ul>

 <h3 id="1">Amendment I</h3>
 <p>Congress shall...</p>
 <h3 id="2">Amendment II</h3>
 <p>A well regulated Militia...</p>
 <h3 id="3">Amendment III</h3>
 <p>No Soldier shall...</p>
 <h3 id="4">Amendment IV</h3>
 <p>The right of the people...</p>
 <h3 id="5">Amendment V</h3>
 <p>No person shall be held...</p>
 <h3 id="6">Amendment VI</h3>
 <p>In all criminal prosecutions...</p>
 <h3 id="7">Amendment VII</h3>
 <p>In Suits at common law...</p>
 <h3 id="8">Amendment VIII</h3>
 <p>Excessive bail shall...</p>
 <h3 id="9">Amendment IX</h3>
 <p>The enumeration in the Constitution...</p>
 <h3 id="10">Amendment X</h3>
 <p>The powers not delegated to the...</p>
</article>

  • [getElementsByTagName][1]返回一个HTMLCollection,而不是元素,所以只有显式地获取第一个元素,才能使用它来获取TOC。(这就是为什么我给了TOC一个标识符)
  • 注释中提到,不应该使用不带var关键字的循环,但没有解释为什么:当没有var关键字时,JS将名称视为属于现有变量(通常是在使用没有该关键字的变量时)。当JS沿着词法层次结构前进时,它最终会到达window,如果不存在,它将使用该名称创建一个变量。在这种情况下,这不是一个大问题,但是如果您曾经处理过许多并发性(在JS中非常常见),for循环可以很容易地开始相互遍历。
  • for循环中立即调用的匿名函数存在,因此循环的每一次迭代在词汇上都与其他迭代分离,因此TOCEntry变量不会发生冲突。(headingText是一个基本字符串,因此将通过值传递,因此不存在冲突的风险。)(此外,事件侦听器现在的行为将更加直观,而不是引用闭包公开的最后一个a值)
  • 标题的it是顺序的,这样您的循环就可以遍历它们,因此document.getElementById(a)将获取与调用它的迭代相关的任何一个。

此外,i是迭代器的伪标准名称;它应该始终是您访问的第一个迭代器(虽然在您的老师上,这就是为什么它不在列表中)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35527544

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档