0

我一直在学习 Udemy Web 开发训练营,我在一个单元中,要求我们制作一个待办事项列表,以便我们练习创建数组、添加和删除数组等等......

我理解讲师编写的代码,并且我已经将我的代码与他的代码进行了比较,但我找不到任何区别。

但是,在他的版本中,当他打开 Chrome 开发人员控制台并将输入输入到提示中时,控制台会做出相应的反应(即返回值或评估表达式),但是当我这样做时,什么也没有出现。

提示框是响应式的,并且可以理解我的条件,因为它会根据我输入的内容而变化,但控制台中没有显示任何内容。

你能帮助我吗?这是我的第一篇 Stack Overflow 帖子,所以请多多关照!

谢谢,杰克

    <!DOCTYPE html>
<html>
 <head>
   <title> To Do List</title>
   <script type="text/javascript" src="script.js"></script>
 </head>
<body>

    <h1>To Do List</h1>

    <ul>
        <li>"New" - Add Item</li>
        <li>"List" - View List</li>
        <li>"Quit" - Quit App</li>
    </ul>




</body>

</html>

    var todos = ["Buy New Turtle"];

var input = prompt("What would you like to do?");

while(input !== "quit"){
    if(input === "list") {
        console.log(todos);
    } else if(input === "new") {
        var newTodo = prompt("Enter new todo");
        todos.push(newTodo);
    }  

    //run code again
    input = prompt("What would you like to do?");
}
console.log("Okay, you Quit the App");
4

2 回答 2

0

嗨,杰克:我已经通过在浏览器中运行测试了您的代码。您面临的问题将通过遵循@Sahee 的建议得到解决。将您的代码移动到 .... 标记之前并包含在其中。

你需要遵守两条规则。

  1. 所有脚本命令应始终介于 ....
  2. 网页中的所有内容,无论是 html、css 还是 javascript,都应该在 .... 标签之间。这些标签表示文档的开始和结束。html 之外的任何内容都可能导致意外行为。
于 2020-08-26T20:24:08.460 回答
0

You have included one js file by putting <script type="text/javascript" src="script.js"></script>, but the code at the bottom of the html file won't work, because it needs to be surrounded by script tag. Just like this:

<script>
    var todos = ["Buy New Turtle"];

var input = prompt("What would you like to do?");

while(input !== "quit"){
    if(input === "list") {
        console.log(todos);
    } else if(input === "new") {
        var newTodo = prompt("Enter new todo");
        todos.push(newTodo);
    }  

    //run code again
    input = prompt("What would you like to do?");
}
console.log("Okay, you Quit the App");
</script>

Also note that the script tag should be inside of html tag.

于 2020-08-26T19:50:35.597 回答