我是网络开发的新手,我需要一个接一个地读取用户输入,因为单击了添加按钮,直到按下停止按钮,然后在列表中显示用户输入。我该怎么做?
<Title>To Do List</Title>
<Body>
<Button id = "AddBtn" onClick = "Store()">Add</Button>
<Button id = "StopBtn" onclick = "Display()">Stop</Button>
<input id = "ip" type = "text" >
<script>
function Store()
{
var tasks;
tasks.push(document.getElementById('ip'));
}
function Display()
{
var i;
for(i=0;i<tasks.length;i++)
{
document.write(tasks[i]);
}
}
</script>
</Body>
答案 0 :(得分:1)
<!Doctype html>
<html>
<head>
<Title>To Do List</Title>
</head>
<Body>
<Button id="AddBtn" onclick="Store()">Add</Button>
<Button id="StopBtn" onclick="Display()">Stop</Button>
<input id="ip" type="text">
<ul id="list">
</ul>
<script>
var tasks = [];
function Store() {
tasks.push(document.getElementById('ip').value);
document.getElementById("ip").value = "";
}
function Display() {
var i;
for (i = 0; i < tasks.length; i++) {
var item = document.createElement("li");
var text = document.createTextNode(tasks[i]);
item.appendChild(text);
document.getElementById("list").appendChild(item);
}
tasks = [];
}
</script>
</Body>
</html>