2

我需要提示访问者三件事,然后用“for循环”和HTML<ol><li>

这就是我所拥有的,但它不起作用:

<script>

var favbook1 = prompt("What is your favotire first book?", "");
var favbook2 = prompt("What is your favotire second book?", "");
var favbook3 = prompt("What is your favotire third book?", "");

books[0] = favbook1;
books[1] = favbook2;
books[2] = favbook3;

for (var i = 0; i < books.lenght ; i++) {
  document.write(books[i] , "<br>")
}

</script>

谢谢您的帮助!

4

2 回答 2

2

我需要提示访问者三件事,然后用 a 和for loopHTML列出它们<ol><li>

您必须先声明books,然后才能为其分配值,并且您有一个错字lenght,应该.length改为。

您的代码也不会创建olwithli元素。如果这是你想要的,你可以做类似的事情:

window.onload = function () {
    var books = [];

    var favbook1 = prompt("What is your favotire first book?", "");
    var favbook2 = prompt("What is your favotire second book?", "");
    var favbook3 = prompt("What is your favotire third book?", "");

    books.push(favbook1);
    books.push(favbook2);
    books.push(favbook3);

    var myList = document.createElement("ol");
    var myListItem;

    for (var i = 0; i < books.length; i++) {
        myListItem = document.createElement("li");
        myListItem.innerHTML = books[i];
        myList.appendChild(myListItem);
    }

    document.body.appendChild(myList);
}

演示-olli每本书


于 2013-02-23T23:39:41.630 回答
1

它应该是

for (var i = 0; i < books.length ; i++) {

编辑:您还应该在 books[0]... 行之前将 books 声明为数组:

var books = new Array();

它也应该是:

document.write(books[i] + "<br />");

于 2013-02-23T23:30:55.797 回答