js搜索文本框

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34343637/article/details/82314117

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>搜索文本框</title>
    <style type="text/css">
    * {
        margin: 0;
        padding: 0;
        border: 0;
    }

    #dv1 {
        width: 500px;
        margin: 300px auto;
    }

    #dv2 {
        height: 42px;
        line-height: 42px;
        width: 100%;
        position: relative;

    }

    #dv2 #searchText {
        width: 448px;
        height: 40px;
        font-size: 18px;
        float: left;
        outline: none;
        border: 1px solid green;
        border-right: 0;
    }

    #btn {
        width: 51px;
        height: 100%;
        font-size: 18px;
        float: left;
        background: rgb(29, 150, 255);
        color: #fff;
        cursor: pointer;
        }

    #dv3{
        background-color: #green;
        border: 1px solid #ccc;
        position: absolute;
        
        width:447px;
        top:0;
        border-top: 0;
        margin-top:40px;
        display: none;
    }
    #dv3 p{
        margin: 0;
        padding: 0;
        height: 30px;
        line-height: 30px;

    }
    </style>
</head>

<body>
    <div id="dv1">
        <div id="dv2">
            <input type="text" name="" id="searchText">
            <input type="submit" name="" id="btn" value="搜索">
            <div id="dv3"></div>
        </div>
    </div>
    <script type="text/javascript">
    var search = ["白日依山尽", "黄河入海流", "欲穷千里目", "更上一成楼", "白白晶晶", "黄黄之水晃晃", "欲网支撑", "更上最新更", "填满地被", "黄沙万里", "天南地北", "我要从南走到北", "还要还要从夜走到黑", "谭维维摆地摊"];

    var text = document.getElementById("searchText");
    text.onkeyup= function() {
         var temshow = document.getElementById("dv3");
        var tempArr = [];

        console.log("顶层的this:" + this);
        //清除上一次的显示列表
        for (var i = 0; i < temshow.childNodes.length; i++) {
                        temshow.removeChild(temshow.childNodes[i]);
                    }

        //遍历数组进行,判断输入框的value值,在所在的arr中元素的位置。假如是第一个则将元素填入
        ////不可以在使用indexOf方法,不可以使用this传值。this 代表的是windows 对象
        search.forEach(function(item) {

            if (item.indexOf(text.value) == 0) {
               
              

                // for (var i = 0; i < temshow.childNodes.length; i++) {
                //         temshow.removeChild(temshow.childNodes[i]);
                //     }
                var getValue = text.value;
               var pObj =  document.createElement("p");
                  
                //判断对话框输入的值,假如为空直接返回。假设不返回,对话框输入空内容适合所有将匹配所有的内容
                if (text.value == "") {
                    
                    // ary.splice(0,ary.length); 
                    temshow.style.display = "none";
                    
                    for (var i = 0; i < temshow.childNodes.length; i++) {
                        temshow.removeChild(temshow.childNodes[i]);
                    }
                    // temshow.childNodes.
                    
                    return;

                }

                 else {
                    tempArr.push(item);
                    console.log(item);


                    
                    temshow.style.display = "block";
                    
                    pObj.innerText =item;
              
                    temshow.appendChild(pObj);
                    
                   

                    //注册鼠标经过与鼠标离开事件
                    pObj.onmouseover=mouseOver;
                    pObj.onmouseout=mouseOut;
                }
            }
        });
         
                     
        
    }

    function mouseOver(){
        this.style.backgroundColor = "#ccc";

    }

    function mouseOut(){
        this.style.backgroundColor = "";
    }

    </script>
</body>

</html>

猜你喜欢

转载自blog.csdn.net/qq_34343637/article/details/82314117