溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

淘寶搜索框的實(shí)現(xiàn)

發(fā)布時(shí)間:2020-06-03 00:13:41 來(lái)源:網(wǎng)絡(luò) 閱讀:952 作者:outsider96 欄目:web開(kāi)發(fā)

實(shí)現(xiàn)淘寶搜索框的效果,首先得去淘寶找端口,然后對(duì)取到的數(shù)據(jù)進(jìn)行處理。

淘寶搜索框的實(shí)現(xiàn)
就如圖所示:
這樣 的數(shù)據(jù)有三層,對(duì)于數(shù)據(jù)的處理要特別注意

css代碼:

#box{position: relative; margin: 30px 300px;}
            *{margin: 0; padding: 0;}
            #txt{width: 500px; height: 30px; border-top-left-radius: 15px; border-bottom-left-radius: 15px; border: 1px solid red;}
            #sou{width: 50px; height: 30px; border: 1px solid red; display: block; position: absolute; left: 496px; top: -5px; text-align: center; line-height: 30px; border-top-right-radius: 15px; border-bottom-right-radius: 15px; background: red;}
            #btn{ border: none; background: red; color: white;}
            #list{width: 500px; border: 1px solid #CECECE; display: none;}
            #list li{list-style: none;}
            #list li:hover{background: #CECECE;}
            #menu{position: absolute; top: 60px; left: 252px; background: #CECECE; width: 250px;}
            #topMenu{width: 250px;}
            #bottomMenu{width: 250px;}
            span{width: 60px; height: 30px; border: 1px solid #000; display: block; float: left; margin: 5px; font-size: 12px; text-align: center; line-height: 30px; cursor: pointer;}
            span:hover{background: red;}
            a{text-decoration: none; color: #000; line-height: 30px;}

html代碼:

<div id="box">
            <input type="text" id="txt" placeholder="女裝"/>
            <span id="sou">
                <button id="btn">搜索</button>
            </span>
            <ul id="list">

            </ul>
            <div id="menu">
                <div id="topMenu"></div>
                <div id="bottomMenu"></div> 
            </div>
        </div>

JavaScript代碼:
let oTxt = document.getElementById("txt");
let oList = document.getElementById("list");
let oMenu = document.getElementById("menu");
let oTop = document.getElementById("topMenu");
let oBot = document.getElementById("bottomMenu");
oTxt.oninput = function() {
var oScript = document.createElement("script");
oScript.src = "https://suggest.taobao.com/sug?code=utf-8&q=" + oTxt.value + "&callback=jsonp&area=c2c";
document.body.appendChild(oScript);
document.body.removeChild(oScript);
}
function jsonp(data) {
//console.log(data)
data1 = data.result;????????????
data2 = data.magic;
let str = "";??????
oList.innerHTML = "";????
for(let i = 0; i < data1.length; i++) {
str += "<li><a href='https://s.taobao.com/search?q=" + data1[i][0] + "'>" + data1[i][0] + "</a></li>";???
}????
//console.log(str);????????
oList.innerHTML = str;
oList.style.display = "block";
var aLi = oList.children;

                for(let i=0; i<data2.length; i++){
                    aLi[data2[i].index - 1].innerHTML += ">";
                    aLi[data2[i].index - 1].onmouseover = function(){
                        var str1 = "";
                        for(var j = 0; j < data2[i].data[0].length; j++){
                            str1 += "<span>"+data2[i].data[0][j].title+"</span>"
                        }

                        oTop.innerHTML = str1;

                        var str2 = "";
                        for(var j = 0; j < data2[i].data[1].length; j++){
                            str2 += "<span>"+data2[i].data[1][j].title+"</span>"
                        }
                        oBot.innerHTML = str2;
                        }
                }
        }
向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI