Use onekeyup event

HTML structure:

<div class="flex-box">
    <span><input type="text" placeholder="Please enter your keywords" id="text"></span>
    <span><input type="button" value="Search"></span>
</div>
<div id="box"></div>
Copy the code

CSS:

    * {
        margin: 0;
        padding: 0;
    }
input:focus {
    outline: none;
}
    .flex-box {
        display: flex;
        justify-content: center;
        align-items: center;
        margin-top: 300px;
    }

    .flex-box span {
        display: inline-flex;
    }

    .flex-box span:nth-child(1) input {
        width: 200px;
        height: 30px;
        border: 1px solid orange;
    }

    .flex-box span:nth-child(2) input {
        width: 50px;
        height: 32px;
        border: 0;
        background: orange;
        color: #fff;
        transition: ease-in .3s;
    }

    .flex-box span:nth-child(2) input:hover {
        background: #ff9511;
    }

    #box {
        display: flex;
        justify-content: center;
        align-items: center;
        position: relative;
    }

    #box div {
        width: 200px;
        border: #ff9511 1px solid;
        border-top-color: white;
        position: relative;
        left: -1.3%;
    }

    #box p {
        padding: 0;
        margin: 0;
        cursor:pointer;
    }
    #box p:hover {
        background: orange;
    }

Copy the code

Here, the style of the element to be generated is written in advance. When creating an element, you can write it directly into the js structure:

    let text = document.getElementById("text");
    let box = document.getElementById("box");
    // Create an array to simulate a database
    let arrays = ["Linux"."What version of Linux is good?"."Linux install QQ"."Does Linux work?".Baidu and Google."Where is Baidu?"."Alibaba"."Has Papa Horse retired?"."When did Father Horse retire?"."And"."How long until the New Year?"];
    // Call the handler directly
    text.onkeyup = keyFun;
    // Create a handler
    function keyFun() {
    // Check if there is an element in the DOM whose id is active and delete it
        if (document.getElementById("active")) {
            box.removeChild(document.getElementById("active"));
        }
        // Use indexOf and push methods
        let txt = this.value;
        let temp = [];
        for (let i = 0; i < arrays.length; i++) {
            if (arrays[i].indexOf(txt) === 0) { temp.push(arrays[i]); }}// end for
        // Check whether the current text box length and temporary array length are 0, true, execute the code block
        if (this.value.length === 0 || temp.length === 0) {
            if (document.getElementById("active")) {
                box.removeChild(document.getElementById("active"));
            }
            return;// Return must be returned, otherwise the generated element cannot be deleted
        }//end if
        // This step generates the element and gives the value of the temporary array traversal to p
        let divObj = document.createElement("div");
        box.appendChild(divObj);
        divObj.id = "active";
        for (let j = 0; j < temp.length; j++) {
            let pObj = document.createElement("p"); divObj.appendChild(pObj); pObj.innerHTML = temp[j]; }}Copy the code