一か念う执着が 2022-09-12 20:23 采纳率: 94.1%
浏览 12
已结题

原理是通过读取本地存储的数据创建元素生成到指定div里面,本地存储的数据有多少就创建多少个li元素 但是不兼容ie

原理是通过读取本地存储的数据创建元素生成到指定div里面,本地存储的数据有多少就创建多少个li元素
但是不兼容ie

 for(let i = 0; i < locaShow.length; i++){
            Showhtml += `<li class='SearchHiy_font'data-key='SearchInputValue' data-index=${i}><span class='Search_font'>${locaShow[i]}</span><span class='SearchiconDelBin'id='delhiy' data-index=${i} data-value=${locaShow[i]}>删除</span></li>`;
        ShowHistory.innerHTML = Showhtml; 
        };


var inputRecommendation = document.getElementsByClassName('inputRecommendation')[0];
var SearchInput = document.getElementById('SearchInput');
var iconDelete = document.getElementsByClassName('icon-delete')[0];
var SearchBtn = document.getElementById('SearchBtn');
var ShowHistory = document.getElementById('ShowHistory');
var rods_del = document.getElementsByClassName('rods_del')[0];
function InputVal(){
    SearchInput.value = '';
}
SearchInput.oninput = function(){
    if(SearchInput.value== ''){
        sh_for_rmd_rods.style.display = 'block';
        inputRecommendation.style.display = '';
        iconDelete.style.display = '';    
    }else{
        inputRecommendation.style.display = 'none';
        iconDelete.style.display = 'block';
        sh_for_rmd_rods.style.display = '';
    };
};
iconDelete.addEventListener('click',function(){
    SearchInput.value='';
    iconDelete.style.display = '';
    inputRecommendation.style.display = '';
    SearchInput.focus();
});
    SearchBtn.addEventListener('click',function(){
        let userVal = SearchInput.value;
        arr = localStorage.getItem('SearchInputValue') ? arr = JSON.parse(localStorage.getItem('SearchInputValue')) : [];
        if(userVal == null||userVal == ''||userVal == undefined||userVal.replace(/(^\s*)|(\s*$)/g, "")==''){
            SearchInput.value = '';
            return false;
        }else{
            let arr = [];
        }
        if(arr.indexOf(userVal)== -1){
            arr.unshift(userVal);
         };
         if(arr.length >= 15){
            arr.splice(arr.indexOf(14),1);
         };
         localStorage.setItem('SearchInputValue',JSON.stringify(arr));
    });
    rods_del.addEventListener('click',function(){
        localStorage.removeItem('SearchInputValue');
        location.reload();
    });
    inputRecommendation.addEventListener('click',function(shwHir){
        SearchInput.focus();
        shwHir.cancelBubble = true;
        let locaShow = JSON.parse(localStorage.getItem('SearchInputValue')) || [];
        if(locaShow.length > ''){
            sh_for_rmd_rods.style.display = 'block';
        }else{
            sh_for_rmd_rods.style.display = '';
        };
        let Showhtml = '';
        for(let i = 0; i < locaShow.length; i++){
            Showhtml += `<li class='SearchHiy_font'data-key='SearchInputValue' data-index=${i}>${locaShow[i]}<span class='SearchiconDelBin'id='delhiy' data-index=${i} data-value=${locaShow[i]}>删除`;
        ShowHistory.innerHTML = Showhtml; 
        };
        if(selectSearch_menu.style.display == 'block'){
            selectSearch_menu.style.display = '';
         }
    });
function loaA(){
    let locaArr = ['连衣裙','男装','新款女装','手机','精品汉服','品牌电脑','华硕'];
    let randomArr =locaArr[Math.floor(Math.random()*locaArr.length)];
    inputRecommendation.innerHTML = randomArr;
};
ShowHistory.addEventListener('click',function(d){
    if(d.target.className == 'SearchiconDelBin'){
        let localDel = JSON.parse(localStorage.getItem('SearchInputValue')) || [];
        var DelSearch = d.target.dataset.index;
        localDel.splice(DelSearch,1);
        localStorage.setItem('SearchInputValue',JSON.stringify(localDel));
        location.reload();
    };
    if(d.target.className == 'SearchHiy_font'){
        let localCon = JSON.parse(localStorage.getItem('SearchInputValue')) || [];
        var inputV = d.target.dataset.index;
            SearchInput.value = localCon[inputV]; 
            if(SearchInput.value == ''){
                inputRecommendation.style.display = 'block';
                iconDelete.style.display = ''; 
            }else{
                inputRecommendation.style.display = 'none';
                iconDelete.style.display = 'block';
            }
};
});

```

  • 写回答

1条回答 默认 最新

  • hfhan_872914334 2022-09-13 11:07
    关注

    你都知道是模板字符串的问题,那你改成可以兼容ie的字符串拼接不就行了

    兼容性问题就是哪里有错改哪里

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 9月21日
  • 已采纳回答 9月13日
  • 创建了问题 9月12日

悬赏问题

  • ¥15 (标签-STM32|关键词-智能小车)
  • ¥20 关于#stm32#的问题,请各位专家解答!
  • ¥15 (标签-python)
  • ¥15 第一个已完成,求第二个做法
  • ¥20 搭建awx,试了很多版本都有错
  • ¥15 java corba的客户端该如何指定使用本地某个固定IP去连接服务端?
  • ¥15 activiti工作流问题,求解答
  • ¥15 有人写过RPA后台管理系统么?
  • ¥15 Bioage计算生物学年龄
  • ¥20 如何将FPGA Alveo U50恢复原来出厂设置哇?