前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【javaScript案例】之搜索的数据显示

【javaScript案例】之搜索的数据显示

作者头像
xinxin-l
发布2022-03-29 08:01:34
6980
发布2022-03-29 08:01:34
举报
文章被收录于专栏:xinxin的随笔记录

今天的效果如下:

这个案例的要点有两个:

==一==是使用CSS显示样式

==二==是使用js比较输入的内容和数组中的内容使得包含输入内容的数据显示出来

首先来看==CSS显示样式==的难点:

两个div的接触部分,要想让它们无缝隙接触,就需要设置float:left

两个div盒子左右两侧的圆角边框,我们需要分别为border-top-left-radius等设置值,这样就大致得到了搜索框的样式,剩下的细节可以去代码中查看~

接着来看==JS进行比较==的部分:

总的思想呢,就是当输入内容时使下方显示搜索框,显示匹配的数据;不输入或输入数据不匹配时,不显示数据或显示暂无数据;搜索框失去焦点时使下方的搜索框消失

  1. 当我们在搜索框中输入内容时,我们可以调用onkeyup函数,先使下方的搜索框display属性值为block; 然后在其中调用forEach遍历数组中的所有数据,通过value获得输入的内容,调用indexOf将该内容与数组中的数据进行比较,若有匹配项的话,其返回值是数组中数据的下标,否则为-1; 若有匹配项的话,我们可以利用innerHTML,在下面的显示框中添加p标签,p中的内容是匹配的数据;如果没有就返回内容是‘暂无数据’的p标签
  2. 当该搜索框失去焦点时,我们令下方搜索框的display属性值为none就可以了

代码如下:

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .container {
            width: 500px;
            height: 160px;
            padding: 40px;
            margin: 100px auto
        }

        #one {
            width: 268px;
            height: 33px;
            float: left;
            border: 0;
            border-top-left-radius: 20px;
            border-bottom-left-radius: 20px;
            background-color: rgb(245, 246, 247);
            outline: none;
        }

        #search {
            background-color: rgb(252, 85, 49);
            color: white;
            width: 70px;
            height: 35px;
            line-height: 35px;
            text-align: center;
            font-size: 13px;
            border-radius: 20px;
            border-top-left-radius: 0;
            border-bottom-left-radius: 0;
            float: left;
        }

        #show {
            width: 270px;
            height: 170px;
            border: 1px solid rgba(77, 76, 76, 0.459);
            display: none;
            margin-top: 40px;
            overflow: hidden;
        }
        #show div{
            width: 100%;
            height: 40px;
            line-height: 40px;
            text-indent: 1em;
            display: block;
        }
        #show div:hover{
            background-color: rgb(240, 240, 245);
            cursor:pointer;
        }
    </style>
</head>

<body>
    <div class="container">
        <div id="nav">
            <input type="text" id="one" placeholder="请输入课程" autocomplete="on">
            <div id="search">搜索</div>
        </div>
        <div id="show">
            <div></div>
        </div>
    </div>

    <script>
        let arr = ['蛋糕便宜卖', '想吃水果', '2333', 'css精品课程','2个小朋友','这儿有2个面包','我们一起','乐队的夏天','天气真好'];
        let one = document.getElementById("one");
        let show = document.getElementById("show")

        one.onfocus = function () {
            show.style.display = "block";
            one.style.border = "1px coral solid"
            one.onkeyup = function () {
                let str = '';
                let tem=false;
                arr.forEach((item) => {
                    let index = item.indexOf(one.value);
                    if (~index) {
                        tem=true;
                        str+='<div>'+item+'</div>';//每次都更新str的值,所以不用担心重复
                    }
                })
                //很重要
                if(one.value=='' || !tem){
                    show.innerHTML='<div>'+'暂无结果'+'</div>';
                }
                else{
                    show.innerHTML=str;
                }
            }

        }
        //onblur 的事件会在对象失去焦点时发生
        one.onblur = function () {
            show.style.display = "none"
            one.style.border = "1px transparent solid"
            show.innerHTML='';
        }
    </script>
</body>

</html>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-02-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档