Это еще одно домашнее задание. Мне нужно немного помочь доработать. Что должно произойти, так это то, что JavaScript просматривает заголовки и создает список. Я больше всего закончил. Мне просто нужна помощь в нескольких вещах. Например: какой ID я помещаю в элемент «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
? Помощь приветствуется.Ошибки при создании таблицы
HTML
<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
<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>
Вы должны технически поставить 'for (var a = ....) ', но javascript немного снисходит в этом, поэтому вы можете избежать этого в этом случае. Другими словами, да 'a' является переменной _actual_, а не просто действует как единое целое. – Rhumborl