1. <h2>历史搜索</h2>
    2. <input type="text" id="app">
    3. <div id="container"></div>
    4. <script>
    5. var historys = []
    6. $("#app").keydown(function(event){
    7. //console.log(event.keyCode)
    8. if(event.keyCode ==13){
    9. /*将值添加到一个数组里面去*/
    10. console.log(Boolean($(this).val()))
    11. var value = $(this).val();
    12. /*只有数组中不包含输入的关键字才向数组添加*/
    13. if(value && !historys.includes(value)){
    14. historys.unshift(value);
    15. console.log(historys)
    16. var template = `
    17. <button>${value}</button>
    18. `
    19. /*渲染数据到页面*/
    20. $("#container").prepend(template)
    21. $(this).val("")
    22. }else{
    23. /*当输入的值数组中存在的时候,让其数组*/
    24. console.log(value)
    25. var index = historys.indexOf(value);
    26. var res = historys.splice(index,1);
    27. historys.unshift(value);
    28. console.log(historys)
    29. /*ui联动*/
    30. $("#container button").eq(index).remove();
    31. $("container").prepend(template)
    32. }
    33. }
    34. })