【发布时间】:2020-12-31 02:54:02
【问题描述】:
我不知道为什么我不能将 LI 项目添加到#historial <UL>。
如果 for 循环在函数内部,则列表会显示一些重复的元素。我几乎可以肯定 for 循环应该是现在的样子,在函数之外。
有人可以帮帮我吗?
let terminos= [];
function agregar(){
let termino = document.querySelector('#busqueda').value;
console.log(termino);
if( terminos.length >= 5){
terminos.shift();
terminos.push(termino);
}else{
terminos.push(termino);
}
console.log(terminos);
document.querySelector('#busqueda').value ='';
}
let boton= document.querySelector('#enviar');
boton.addEventListener('click', agregar);
for( let i =0 ; i< terminos.length; i++){
document.querySelector('#historial').innerHTML += `<li> ${terminos[i]}</li>`;
}
#historial{
background-color: tomato;
width:50vw;
height:300px;
color:white;
font-size:20px;
}
<label for="busqueda"> Término</label>
<input type="text" id="busqueda"> <br>
<button type="submit" id="enviar"> Enviar</button>
<p> UL#historial</p>
<ul id="historial">
</ul>
【问题讨论】:
标签: javascript html dom