一か念う执着が 2022-08-20 10:33 采纳率: 94.1%
浏览 35
已结题

我是想把localStorage里面的数据显示到ul里面, 尝试过多次没有成功 以下是html和JavaScript的代码 希望能帮助我解决不显示的问题

我是想把localStorage里面的数据显示到ul里面,
尝试过多次没有成功
以下是html和JavaScript的代码
希望能帮助我解决不显示的问题


```html
<div id="sh_for_rmd_rods">
                        <div class="rods_font-del_box">
                                <span class="rods_font">搜索历史
                                <span id="Rod_Del" class="rods_del">全部删除
                        </div>
                        <ul id="ShowHistory">
                            
                        
                </div>


```javascript
 SearchBtn.onclick = function(){
        let userVal = SearchInput.value;
        let localCon = localStorage.getItem('SearchInputValue');
        arr = localStorage.getItem('SearchInputValue') ? arr = JSON.parse(localStorage.getItem('SearchInputValue')) : [];
        if(userVal == null||userVal == ''||userVal == undefined||userVal.replace(/(^\s*)|(\s*$)/g, "")==''){
            return;
        }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));
         showLog();
        };
        rods_del.onclick = function () {
            localStorage.removeItem('SearchInputValue');
        };
         ShowHistory.addEventListener('click', function(e){
            if(e.target.nodeName == 'Li'){
                let datas = JSON.parse(localStorage.getItem('SearchInputValue'));
                console.log(e.target.dataset['index']);
                datas.splice(e.target.dataset['index'],1);
                localStorage.setItem('SearchInputValue',JSON.stringify(datas));
                showLog();
            };
         });
         function showLog(){
            let locals = JSON.parse(localStorage.getItem('SearchInputValue')) || [];
            let showHistorysHtml = '';
            for(let i = 0; i <locals.length; i++){
                showHistorysHtml += '
  • +locals[i]+
  • '
    ; } ShowHistory.innerHTML = showHistorysHtml; };
    
    
    • 写回答

    1条回答 默认 最新

    • 雾里桃花 2022-08-20 11:29
      关注

      在这个函数中打印一下showHisorysHtml看看有值吗

      img

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

    报告相同问题?

    问题事件

    • 已结题 (查看结题原因) 8月22日
    • 已采纳回答 8月21日
    • 创建了问题 8月20日

    悬赏问题

    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题