欢迎访问宙启技术站
智能推送

jquery插件怎么实现搜索历史

发布时间:2023-05-14 03:51:15

jQuery插件是一种非常强大的前端技术,能够帮助开发者快速构建复杂的交互效果和功能。其中,搜索历史功能是非常实用的一个功能,能够帮助用户记录之前搜索过的关键词,方便后续再次搜索。下面,我们就来介绍一下如何使用jQuery插件来实现搜索历史功能。

一、插件设计思路

在实现搜索历史功能之前,我们需要首先确定插件的设计思路。一般来说,搜索历史功能可以分为以下几个步骤:

1.用户输入搜索关键词。

2.用户点击搜索按钮,或按下回车键,提交搜索请求。

3.插件将搜索关键词保存到客户端本地存储中。

4.插件从客户端本地存储中读取搜索历史列表,并显示给用户。

5.用户可以点击搜索历史列表中的关键词,再次进行搜索。

基于这个思路,我们可以开始设计插件的具体实现。

二、插件功能分析

在设计插件之前,我们先来分析一下插件需要完成哪些功能。

1.插件需要有一个输入框和一个搜索按钮,用于用户输入搜索关键词。

2.插件需要能够监听用户的输入事件以及搜索按钮的点击事件,并能够响应用户的操作。

3.插件需要能够将用户输入的搜索关键词保存到客户端本地存储中,并能够读取本地存储中的搜索历史列表。

4.插件需要能够将搜索历史列表显示给用户,并且能够响应用户点击搜索历史关键词的操作。

5.插件需要具有良好的用户体验,如搜索历史列表的样式、输入框的清空等方面。

基于上述分析,我们可以开始实现插件了。

三、插件实现

1.创建HTML页面

首先,我们需要创建一个HTML页面,用于展示搜索功能和搜索历史列表,代码如下:

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>搜索历史插件</title>
	<link rel="stylesheet" href="style.css">
</head>
<body>
	<div class="search-box">
		<input type="text" id="txtSearch" placeholder="请输入搜索关键词">
		<button id="btnSearch">搜索</button>
	</div>
	<div class="history-list">
		<ul id="ulHistory">
		</ul>
	</div>
	<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
	<script src="history.js"></script>
</body>
</html>

我们在页面中创建了一个包含输入框和搜索按钮的search-box Div容器,以及一个包含搜索历史列表的history-list容器。

2.创建CSS样式表

为了让页面看起来更加美观,我们需要为其添加一些CSS样式,代码如下:

.search-box {
    margin: 20px auto;
    width: 600px;
    height: 30px;
}

.search-box input[type=text] {
    width: 400px;
    height: 30px;
    border: 1px solid #ccc;
    padding: 5px;
    float: left;
}

.search-box button {
    height: 32px;
    line-height: 32px;
    width: 80px;
    text-align: center;
    background-color: #5095e1;
    color: #fff;
    border: 0;
    float: right;
}

.history-list {
    margin: 20px auto;
    width: 600px;
    height: 200px;
}

.history-list ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
}

.history-list li {
    line-height: 30px;
    cursor: pointer;
    border-bottom: 1px solid #ccc;
}

.history-list li:hover {
    background-color: #f1f1f1;
}

我们在样式表中定义了search-box和history-list两个Div容器的样式,以及输入框和按钮、搜索历史列表等元素的样式。

3.编写JavaScript代码

接着,我们需要编写插件的JavaScript代码。我们将所有的代码都封装在一个名为history.js的文件中,代码如下:

$(document).ready(function () {
    InitSearch();
    InitHistory();
});

// 初始化搜索框
function InitSearch() {
    $("#btnSearch").click(function () {
        var keyword = $("#txtSearch").val();
        if (keyword.trim() !== "") {
            SaveHistory(keyword);
            Search(keyword);
        }
    });

    $("#txtSearch").keydown(function (event) {
        if (event.keyCode === 13) {
            var keyword = $("#txtSearch").val();
            if (keyword.trim() !== "") {
                SaveHistory(keyword);
                Search(keyword);
            }
        }
    });
}

// 初始化搜索历史列表
function InitHistory() {
    var historyList = ReadHistory();
    if (historyList === null || historyList.length === 0) {
        return;
    }

    var html = "";
    for (var i = 0; i < historyList.length; i++) {
        html += "<li>" + historyList[i] + "</li>";
    }

    $("#ulHistory").html(html);

    $("#ulHistory li").click(function () {
        var keyword = $(this).text();
        $("#txtSearch").val(keyword);
        Search(keyword);
    });

}

// 保存搜索历史
function SaveHistory(keyword) {
    var historyList = ReadHistory();
    if (historyList === null) {
        historyList = [];
    }

    var index = historyList.indexOf(keyword);
    if (index > -1) {
        historyList.splice(index, 1);
    }

    historyList.unshift(keyword);

    if (historyList.length > 10) {
        historyList.pop();
    }

    localStorage.setItem("history_list", JSON.stringify(historyList));
}

// 读取搜索历史
function ReadHistory() {
    var historyList = localStorage.getItem("history_list");
    if (historyList === null || historyList === "") {
        return null;
    }

    return JSON.parse(historyList);
}

// 执行搜索
function Search(keyword) {
    console.log("搜索关键词:" + keyword);
}

上面的代码中,我们定义了InitSearch、InitHistory、SaveHistory、ReadHistory和Search这5个函数。这些函数分别用于初始化搜索框、初始化搜索历史、保存搜索历史、读取搜索历史和执行搜索操作。

下面,我们将逐一解释各个函数的作用。

①InitSearch函数:

初始化搜索框。该函数会监听搜索按钮的点击事件和输入框的keydown事件,当用户点击搜索按钮或按下回车键时,会先判断输入框中是否有关键词,如果有则调用SaveHistory函数保存搜索关键词,并执行Search函数进行搜索操作。

②InitHistory函数:

初始化搜索历史列表。该函数会先调用ReadHistory函数读取本地存储中的搜索历史列表,然后将列表中的每一项都添加到搜索历史ul元素中。并为每一项都添加click事件,当用户点击某个搜索历史项时,会调用Search函数重新搜索该关键词。

③SaveHistory函数:

保存搜索历史。该函数在用户提交搜索关键词之后会被调用。它会先调用ReadHistory函数读取本地存储中的历史记录列表,然后将用户输入的关键词添加到列表的最前面,并且判断列表长度是否超过了10条,如果超过了,就删除最后一条记录。最后,它会将更新后的列表再次保存到本地存储中。

④ReadHistory函数:

读取搜索历史。该函数会从本地存储中读取搜索历史记录信息,并将其转换为JavaScript对象。如果本地存储中不存在记录,则返回null。

⑤Search函数:

执行搜索操作。该函数会接收用户输入的