<h2>历史搜索</h2><input type="text" id="app"><div id="container"></div><script>var historys = []$("#app").keydown(function(event){//console.log(event.keyCode)if(event.keyCode ==13){/*将值添加到一个数组里面去*/console.log(Boolean($(this).val()))var value = $(this).val();/*只有数组中不包含输入的关键字才向数组添加*/if(value && !historys.includes(value)){historys.unshift(value);console.log(historys)var template = `<button>${value}</button>`/*渲染数据到页面*/$("#container").prepend(template)$(this).val("")}else{/*当输入的值数组中存在的时候,让其数组*/console.log(value)var index = historys.indexOf(value);var res = historys.splice(index,1);historys.unshift(value);console.log(historys)/*ui联动*/$("#container button").eq(index).remove();$("container").prepend(template)}}})
