【发布时间】:2017-11-16 01:24:29
【问题描述】:
这个文件是一个简单的列表生成器。它从 JS 数组的 div 中显示的三个示例项目开始。 onclick 按钮一个“推”出现提示,用户添加的任何内容都会添加到 JS 中的数组中。根据控制台,这可以正常工作。第二个按钮“shift”将第一项从列表中拉出(传统的做列表样式),因为用户一次做一个项目。push 和 shift 按钮都运行相应的 JS 函数,可以完美地完成工作。在控制台中,每次使用 wither 时都会更新数组。
我的问题:一旦通过按钮对数组“toDoList”进行了修改,我如何让 div 刷新数组“toDoListJoined”???
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>JavaScript To Do List</title>
</head>
<body id="home">
<button id="push" onclick="pushPrompt()">Add something to the end of your list</button>
<button id="shift" onclick="shiftIt()">Remove the first item from your list</button>
<p id="listPlace">
<!--Here's where the magic happens-->
</p>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
let toDoList = ["sample item 1", "sample item 2", "sample item 3"];
let toDoListJoined = toDoList.join("<br>");
let addItem = "";
document.getElementById("listPlace").innerHTML = toDoListJoined;
function pushPrompt() {
addItem = prompt("What is the item you want to add to your list?", "Sample item");
toDoList.push(addItem);
}
function shiftIt() {
toDoList.shift();
}
</script>
</body>
</html>
【问题讨论】:
标签: javascript arrays