一か念う执着が 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日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效