localStorage()历史搜索记录

103 篇文章 0 订阅
97 篇文章 0 订阅
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>首页</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        body{
            margin-left: 300px;

        }
        ul{
            list-style: none;
            width: 250px;
            position: absolute;


        }
        ul li,div{
            width: 250px;
            padding: 10px 0;
            margin-left: 10px;
            border-bottom: 1px dashed #ccc;
            height: 20px;
        }
        a{
            float: right;
        }
        input{
            padding: 5px;
            margin: 10px;
        }
    </style>
</head>
<body>
<input type="search" placeholder="输入搜索关键字"/>
<input type="button" value="搜索"/>
<div><a href="javascript:;">清空搜索记录</a></div>
<ul>
    <!-- <li>没有搜索记录</li>
    <li><span>手机</span><a href="javascript:;">删除</a></li>
</ul>
<script src="jquery.min.js"></script>
<script>
   $(function () {
        // 根据历史记录渲染历史列表
        // 获取localStorage数据数据是json格式
        var historyListJson = localStorage.getItem('historyList') || '[]'; //historyList预设的键;
        //把json数据转换成数组
        var historyListArr = JSON.parse(historyListJson);
        
        // 1渲染数据
        function render(){
            // 定义一个空html
            var html = '';

            // 遍历数组

            // 方法一
            // historyListArr.forEach(function(item,i){ //value == key
            //     html += '<li><span>'+ item +'</span><a data-index="'+ i +'" href="javascript:;">删除</a></li>';
            // });

            // 方法二
            for(var i=0;i < historyListArr.length;i++){
                html += '<li><span>'+ historyListArr[i] +'</span><a data-index="'+ i +'" href="javascript:;">删除</a></li>';
            }
            // 判断html里面有数据没
            html = html||'<li>没有搜索记录</li>';
            // 把数据渲染到ul里面
            $('ul').html(html);
        }
        render();

        // 2点击搜索的时候更新历史搜索记录、
        $('[type="button"]').on('click',function(){
            // 获取搜索框的内容
            var key = $.trim($('[type="search"]').val());
            // console.log(key);

            // 判断点击搜索、搜索框内没有内容提示用户
            if(!key){
                alert('请输入内容');
                return false;
            }  

            // 追加数据到historyListArr数组中
            historyListArr.push(key);
            
            // 保存更新追加的数据到json数据中
            localStorage.setItem('historyList',JSON.stringify(historyListArr));

            // 渲染数据/直接调用前面的渲染数据函数
            render();

            // 清空搜索框
            $('[type="search"]').val('');
        })

        // 3删除数据
        // 因为a的id是动态生成的需要冲ul拿到a的id
        $('ul').on('click','a',function(){
            // 获取点击的a的id
            var index = $(this).data('index');

            // 删除数组内的指定位置数据
            historyListArr.splice(index,1);

            // 保存更新追加的数据到json数据中
            localStorage.setItem('historyList',JSON.stringify(historyListArr));

            // 渲染数据/直接调用前面的渲染数据函数
            render();

        });

        // 清除全部历史记录
        $('div a').on('click',function(){
            // 清空数据
            historyListArr = []; //先把数组设置为空
           
            localStorage.removeItem('historyList'); //再删除空数据
            
            // 渲染数据/直接调用前面的渲染数据函数
            render();

        });
    
    });

</script>
</body>
</html>
若要在 Mozilla Firefox 中实现搜索栏的历史记录,你可以使用 Web Storage API 提供的 localStorage 或 sessionStorage 功能来存储搜索历史数据。以下是一个示例代码: ```javascript // 保存搜索记录 function saveSearchTerm(term) { if (typeof(Storage) !== "undefined") { // 使用 localStorage 存储搜索记录 const searchHistory = localStorage.getItem("searchHistory"); let historyArray = []; if (searchHistory) { historyArray = JSON.parse(searchHistory); } historyArray.push(term); localStorage.setItem("searchHistory", JSON.stringify(historyArray)); } else { console.log("Web Storage is not supported."); } } // 获取搜索历史记录 function getSearchHistory() { if (typeof(Storage) !== "undefined") { // 使用 localStorage 获取搜索记录 const searchHistory = localStorage.getItem("searchHistory"); if (searchHistory) { const historyArray = JSON.parse(searchHistory); // 遍历搜索记录并处理 for (let i = 0; i < historyArray.length; i++) { const record = historyArray[i]; // 在这里进行处理,例如显示到页面上 console.log(record); } } else { console.log("No search history found."); } } else { console.log("Web Storage is not supported."); } } ``` 在上述示例中,`saveSearchTerm` 函数用于保存搜索条目。它首先检查浏览器是否支持 Web Storage API,然后使用 localStorage 来存储搜索记录。如果之前已经有搜索记录,它将先获取现有的记录数组,然后将新的搜索条目追加到数组中,并将更新后的数组存储回 localStorage。 `getSearchHistory` 函数用于获取搜索历史记录。它也首先检查浏览器是否支持 Web Storage API,然后使用 localStorage 获取搜索记录。如果存在搜索记录,它将解析 JSON 字符串为数组,然后遍历数组并处理每个搜索记录(在示例中只是简单地打印到控制台)。 请注意,这些代码只是一个基本示例,实际情况中你可能需要根据自己的需求进行适当的修改和扩展。另外,这些代码只适用于在客户端存储搜索历史记录,如果你需要在服务器端进行存储和管理,需要使用其他的技术和方案。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值