我试图在每个元素前面返回带有索引的数组,并在每个元素之后删除逗号。我能够将每个元素返回到一个新行,push()
但仍然无法获得编号列表。我已经尝试在我的 js 中使用<li>
and 。我在这里想念什么?<ol>
<div>
// TODO keep the student list state in a global list
var roster = new Array("");
function addStudent() {
// TODO lookup the user entered text
var newStudent = document.getElementById("student").value;
// TODO store the new student name in global list
roster.push("<div>" + newStudent + "</div>");
// TODO render the entire list into the output div
roster.innerHTML = roster.join('');
document.getElementById("output").innerHTML = "Student List" + roster;
return false;
}
function init() {
// TODO register the onsubmit for 'theForm' to use addStudent
if (document && document.getElementById) {
document.getElementById('theForm').onsubmit = addStudent;
}
}
window.onload = init;
<form action="#" method="post" id="theForm" novalidate>
<fieldset>
<legend>Enter a student name to add to the roster</legend>
<div><label for="student">Student Name</label><input type="text" name="student" id="student"></div>
<input type="submit" value="Add to Roster" id="submit">
<div id="output"></div>
</fieldset>
</form>
<!-- <script src="js/students.js"></script> -->