一か念う执着が 2022-08-23 22:03 采纳率: 94.1%
浏览 36
已结题

想通过读取localStorage(SearchInputValue)里面的元素,然后随机选择一个元素展示

想通过读取localStorage(SearchInputValue)里面的元素,然后随机选择一个元素展示在(

臭豆腐
)里面
触发规则是网页刷新时触发事件,刷新一次更新一次只要localStorage存在元素,就刷新一次更新一次,展示不同的元素

img


 <div id="SearchBox">
                <div class="icnb-search">
                    <div id="SearchEenu">
                        <div class="SearchFont">
                            <span class="menu-baby">宝贝</span>
                            <span class="icon-se">
                                <span class="icon-font">ˇ</span>
                            </span>
                        </div>
                        <div id="selectSearch-menu">
                            <div id="SearchTmall"><span class="select-tamll">天猫</span></div>
                            <div id="SearchShop"><span class="select-shop">店铺</span></div>
                        </div>
                    </div>
                    <form action="" id="searchBox">
                        <div class="inputRecommendation" data-Rdr-type="SearchRdN">臭豆腐</div>
                          <div class="inputBox">
                            <input id="SearchInput" aria-label="请输入内容" name="SearchInput" autocomplete="off">
                          </div>
                          <div class="icon-delete">×</div> 
                          <div id="SearchButton">
                            <button id="SearchBtn">搜索</button>
                          </div> 
                    </form>
                </div>
                <div id="Search_rn_box">
var SearchEenu = document.getElementById('SearchEenu');
var selectSearch_menu = document.getElementById('selectSearch-menu');
var sh_for_rmd_rods = document.getElementById('sh_for_rmd_rods');
SearchEenu.onclick = function(ent){
    if(selectSearch_menu.style.display==''){
        selectSearch_menu.style.display = 'block';
        ent.cancelBubble = true;
    }else{
        selectSearch_menu.style.display = '';
        ent.cancelBubble = true;
    };
};
document.onclick = function(shwEnt){
     if(selectSearch_menu.style.display == 'block'){
        selectSearch_menu.style.display = '';
     }
     if(sh_for_rmd_rods.style.display == 'block'){
        sh_for_rmd_rods.style.display = '';
    }
};
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];
SearchInput.oninput = function(){
    if(SearchInput.value==''){
        inputRecommendation.style.display = '';
        iconDelete.style.display = '';
        sh_for_rmd_rods.style.display = 'block';
    }else{
        inputRecommendation.style.display = 'none';
        iconDelete.style.display = 'block';
        sh_for_rmd_rods.style.display = '';
    };
};
iconDelete.onclick = function(){
    SearchInput.value='';
    iconDelete.style.display = '';
    inputRecommendation.style.display = '';
    SearchInput.focus();
};
    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, "")==''){
            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.onclick = function () {
        localStorage.removeItem('SearchInputValue');
        location.reload();
    };
    inputRecommendation.onclick = 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; 
        };
};
ShowHistory.onclick = 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();
    };
};

  • 写回答

1条回答 默认 最新

  • 雾里桃花 2022-08-23 22:32
    关注

    获取到SearchInputValue这个数组
    通过以下代码随机出数组中的内容进行展示
    let index = parseInt(Match.random() * SearchInputValue.length)
    console.log(SearchInputValue[index])

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

报告相同问题?

问题事件

  • 系统已结题 9月1日
  • 已采纳回答 8月24日
  • 创建了问题 8月23日

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭