Home > Web Front-end > JS Tutorial > js implements a search function similar to Lenovo keywords (code attached)

js implements a search function similar to Lenovo keywords (code attached)

不言
Release: 2018-08-13 16:14:31
Original
4210 people have browsed it

The content of this article is about realizing a search function similar to Lenovo keywords in js (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Page code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title>js/jQuery实现类似百度搜索功能</title>
  <meta name="Author" content="Michael">
  <meta name="Keywords" content="js/jQuery实现类似百度搜索功能">
  <meta name="Description" content="js/jQuery实现类似百度搜索功能,可用键盘控制">
  <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
  <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.js"></script>
  <style type="text/css">
    #container{
        position:absolute;
        left:50%;
        top: 40%;
    }
    #content{
        float:left;
        position:relative;
        right:50%;
    }
    input{
        border:0;
        width:288px;
        height:30px;
        font-size:16px;
        padding:0 5px;
        line-height:30px;
    }
    .item{
        padding:3px 5px;
        cursor:pointer;
    }
    .addbg{
        background:#87A900;
    }
    .first{
        border:solid #87A900 2px;
        width:300px;
    }
    #append{
        border:solid #87A900 2px;
        border-top:0;
        display:none;
    }
  </style>
 </head>
 <body>
    <p id="container">
    <p id="content">
        <p class="first"><input id="kw" onKeyup="getContent(this);" /></p>
        <p id="append"></p>
    </p>
    </p>
 </body>
</html>
Copy after login


2. js code:

<script type="text/javascript">
var data = [
    "你好,我是Michael",
    "你是谁",
    "你最好啦",
    "你最珍贵",
    "你是我最好的朋友",
    "你画我猜",
    "你是笨蛋",
    "你懂得",
    "你为我着迷",
    "你是我的眼"
];
$(document).ready(function(){
    $(document).keydown(function(e){
        e = e || window.event;
        var keycode = e.which ? e.which : e.keyCode;
        if(keycode == 38){
            if(jQuery.trim($("#append").html())==""){
                return;
            }
            movePrev();
        }else if(keycode == 40){
            if(jQuery.trim($("#append").html())==""){
                return;
            }
            $("#kw").blur();
            if($(".item").hasClass("addbg")){
                moveNext();
            }else{
                $(".item").removeClass(&#39;addbg&#39;).eq(0).addClass(&#39;addbg&#39;);
            }
           
        }else if(keycode == 13){
            dojob();
        }
    });
    var movePrev = function(){
        $("#kw").blur();
        var index = $(".addbg").prevAll().length;
        if(index == 0){
            $(".item").removeClass(&#39;addbg&#39;).eq($(".item").length-1).addClass(&#39;addbg&#39;);
        }else{
            $(".item").removeClass(&#39;addbg&#39;).eq(index-1).addClass(&#39;addbg&#39;);
        }
    }
   
    var moveNext = function(){
        var index = $(".addbg").prevAll().length;
        if(index == $(".item").length-1){
            $(".item").removeClass(&#39;addbg&#39;).eq(0).addClass(&#39;addbg&#39;);
        }else{
            $(".item").removeClass(&#39;addbg&#39;).eq(index+1).addClass(&#39;addbg&#39;);
        }
       
    }
   
    var dojob = function(){
        $("#kw").blur();
        var value = $(".addbg").text();
        $("#kw").val(value);
        $("#append").hide().html("");
    }
});
function getContent(obj){
    var kw = jQuery.trim($(obj).val());
    if(kw == ""){
        $("#append").hide().html("");
        return false;
    }
    var html = "";
    for (var i = 0; i < data.length; i++) {
        if (data[i].indexOf(kw) >= 0) {
            html = html + "<p class=&#39;item&#39; onmouseenter=&#39;getFocus(this)&#39; onClick=&#39;getCon(this);&#39;>" + data[i] + "</p>"
        }
    }
    if(html != ""){
        $("#append").show().html(html);
    }else{
        $("#append").hide().html("");
    }
}
function getFocus(obj){
    $(".item").removeClass("addbg");
    $(obj).addClass("addbg");
}
function getCon(obj){
    var value = $(obj).text();
    $("#kw").val(value);
    $("#append").hide().html("");
}
</script>
Copy after login


3. Operation effect:
                                                                                        #1. Enter keywords in the input box :

2. Use the up and down keys on the keyboard to select:

3.Enter key input:

##Related recommendations:

How js uses the setInterval timer method to implement a carousel chart (complete code)

js encapsulates the _new function and how to implement the new keyword (with code test)

The above is the detailed content of js implements a search function similar to Lenovo keywords (code attached). For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template