[英]How to insert items into an array and display them inside a div in JavaScript
I have a problem with array in Javascript. 我在Javascript中遇到数组问题。 I have a button that pushes an item to an array.
我有一个按钮,可将项目推到数组。 After I click the button, the for loop which is intended to display a list of items in a div isn't executed.
单击按钮后,用于显示div中项目列表的for循环未执行。
Below is the code: 下面是代码:
HTML: HTML:
<input type="text" id="tb">
<button id="btn">Submit</button>
<div id="list"></div>
Javascript: Javascript:
var list = [];
btn.onclick = function(){
list.push(document.getElementById("tb").value);
}
for (var i in list){
document.getElementById("list").innerHTML = "<p>"+list[i]+"</p>"
}
Are there any solutions for this such that after I click the button, the div updates the list of items, much like a to-do list? 有什么解决方案可以使我单击按钮后div更新项目列表,就像待办事项列表一样吗?
You can just append the html to the list when clicking the button - see demo below: 您可以在单击按钮时将 html 附加到列表中-参见下面的演示:
btn.onclick = function() { document.getElementById("list").innerHTML += "<p>" + document.getElementById("tb").value + "</p>" }
<input type="text" id="tb"> <button id="btn">Submit</button> <div id="list"></div>
If you want to use the array as a data store as you say in the comments to this answer, you may do something like given in the snippet below: 如果您要像在此答案的注释中所说的那样将数组用作数据存储 ,则可以执行以下代码片段中给出的操作:
var list = []; btn.onclick = function() { // add to the list list.push(document.getElementById("tb").value); // TODO: save to local storage if needed // reset the list document.getElementById("list").innerHTML = ''; // display the list for (var i in list) { document.getElementById("list").innerHTML += "<p>" + list[i] + "</p>"; } }
<input type="text" id="tb"> <button id="btn">Submit</button> <div id="list"></div>
i'd do: 我会做:
var list = [];
btn.onclick = function(){
list.push(document.getElementById("tb").value);
for (var i in list){
document.getElementById("list").innerHTML = "<p>"+list[i]+"</p>"
}
}
As you can see the for loop must be triggered in its execution so i moved it inside the function 如您所见,for循环必须在其执行中被触发,因此我将其移入了函数中
Just append a new <p>
element to the list instead of using innerHTML
. 只需将一个新的
<p>
元素添加到列表中即可,而不是使用innerHTML
。 Also, you should bind event listeners using addEventListener
: 另外,您应该使用
addEventListener
绑定事件侦听器:
var list = document.getElementById('list'); var input = document.getElementById('tb'); document.getElementById('btn').addEventListener('click', function () { var p = document.createElement('p'); p.textContent = input.value; list.appendChild(p); });
<input type="text" id="tb"> <button id="btn">Submit</button> <div id="list"></div>
var list = []; var btn = document.getElementById("btn"); btn.onclick = function(){ var tb = document.getElementById("tb").value; //list.push(tb); document.getElementById("list").innerHTML += "<p>"+tb+"</p>" }
<input type="text" id="tb"> <button id="btn">Submit</button> <div id="list"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.