gpt4 book ai didi

Javascript:我需要提示访问者三件事,然后用 "for loop"列出它们

转载 作者:行者123 更新时间:2023-11-29 19:59:31 27 4
gpt4 key购买 nike

我需要提示访问者三件事,然后用“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>

感谢您的帮助!

最佳答案

I need to prompt a visitor for three things and then list them with a for loop and the HTML <ol> and <li>

你必须申报books首先,在你可以给它赋值之前,你在 lenght 中有错字应该是 .length相反。

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

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

DEMO - 创建olli每本书


关于Javascript:我需要提示访问者三件事,然后用 "for loop"列出它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15046938/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com