百度搜索

百度搜索

<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .block {
            width: 600px;
            height: 35px;
            border: 1px solid #000;
            margin: auto;
            position: absolute;
            left: 0;
            top: 0;
            right: 0;
            bottom: 0;
            font-size: 0;
        }

        .block > input {
            position: absolute;
            left: 0;
            top: 0;
            border-style: none;
            outline: none;
            box-sizing: border-box;
            width: 500px;
            height: 35px;
            font-size: 1rem;
            padding-left: 0.5rem;
        }

        .block > span {
            position: absolute;
            right: 0;
            font-size: 1rem;
            display: inline-block;
            width: 100px;
            height: 35px;
            color: #fff;
            background: #15a0ff;
            text-align: center;
            line-height: 35px;
        }

        .nav {
            font-size: 0.9rem;
            list-style: none;
            position: absolute;
            top: 35px;
            left: -1px;
            width: 500px;
            border: 1px solid #979797;
        }

        .nav > li {
            padding-left: 0.5rem;
            line-height: 30px;
        }

        .nav > li:hover {
            background: #e4e4e4;
        }

        .nav a {
            color: #000;
            font-weight: bold;
            text-decoration: none;
        }

        .bgcolor {
            background: #e4e4e4;
        }
    </style>
</head>
<body>
<div class="block">
    <input id="ipt" type="text"/>
    <span id="btn">百度一下</span>
    <ul class="nav">
    </ul>
</div>
<script>
    /*
     * https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=
     * https://www.baidu.com/s?wd=
     *
     * */
    var script = null;
    var isshu = false;
    var index = 0;
    var ipt = document.getElementById("ipt");
    var btn = document.getElementById("btn");
    var nav = document.getElementsByClassName("nav")[0];
    var api = " https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su";
    ipt.addEventListener("keyup", getInput(api));



    ipt.addEventListener("keydown", function (e) {
        if (e.keyCode == 38 || e.keyCode == 40) {
            e.preventDefault();//处理事件默认行为
        }
    })
    btn.addEventListener("click", function () {
        location.href = "https://www.baidu.com/s?wd=" + ipt.value;
    })
    document.body.addEventListener("keyup", function (e) {
        if (!nav.children.length)return;
        if (e.keyCode == 38) {
            isshu = true;
            index--;
            if (index < 0) {
                index = nav.children.length - 1;
            }
            nav.children[index + 1 > nav.children.length - 1 ? 0 : index + 1].className = "";
            nav.children[index].className = "bgcolor";
            ipt.value = nav.children[index].innerText;
        }
        else if (e.keyCode == 40) {
            isshu = true;
            if (document.getElementsByClassName("bgcolor").length) {
                index++;
            }
            if (index > nav.children.length - 1) {
                index = 0;
            }
            nav.children[index - 1 < 0 ? nav.children.length - 1 : index - 1].className = "";
            nav.children[index].className = "bgcolor";
            ipt.value = nav.children[index].innerText;
        }
    });




    function getInput(src) {
        var t;
        return function () {
            isshu = false;
            var that = this,
                    args = arguments;
            clearTimeout(t);
            t = setTimeout(function () {
                //jsonp ipt.value
                jsonp.apply(that, [args, src])
            }, 500);
        }
    }

    //封装一个jsonp方法
    function jsonp() {
        if (isshu) {
            return;
        }
        //动态创建
        if (script) {
            script.remove();
        }
        script = document.createElement("script");
        script.src = arguments[1] + "?" + "wd=" + this.value + "&cb=getData";
        document.body.appendChild(script);
    }
    //回调函数
    function getData(res) {
        nav.innerHTML = "";
        res.s.forEach(function (val, index) {
            var lilist = document.createElement("li");
            lilist.innerHTML = "<a href='https://www.baidu.com/s?wd=" + val + "'>" + val + "</a>";
            nav.appendChild(lilist);
        });
        index = 0;
    }
</script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值