利用百度接口,通过jsonp实现跨域访问,完成搜索提示框

先来效果图

图片一:

图片二:

代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        center{
            line-height: 40px;
            font-size: 20px;
        }
        .father{
            width:500px ;
        }
        .searchFath{
            padding-top: 200px;
        }
        .searchIn{
            width: 400px;
            height: 40px;
            font-size: 20px;
        }
        .searchBtn{
            height: 40px;
            width: 50px;
            text-align: center;
        }
        ul{
           list-style: none;
        }
        li{

        }
    </style>
</head>
<body>
<center>
    <div class="father">
        <div class="searchFath">
            <input class="searchIn">
            <button class="searchBtn">查询</button>
        </div>
        <div class="content"></div>
    </div>
</center>

<script>
    //搜索框
    var searchIn;
    //button按钮
    var searchBtn;
    //上一次搜索的文字

    init();

    function init() {
        searchIn=document.querySelector('.searchIn');
        //给input添加节流搜索
        searchIn.addEventListener('input',()=>{
            setTimeout(()=>{
                creatJsonp(searchIn.value);
            },1000)
        });
        searchBtn=document.querySelector('.searchBtn');
        searchBtn.addEventListener('click',()=>search(searchIn.value));
        document.addEventListener('keydown',(e)=>{
            if (e.key === 'Enter') {
                search(searchIn.value);
            }
        });
    }
    //创建jsonp请求
    function creatJsonp(word) {
        var script=document.createElement("script");
        script.src="https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd="+word+"&json=1&p=3&sid=22084_1436_13548_21120_22036_22073&req=2&csor=0&cb=callback";
        document.body.appendChild(script);
        script.addEventListener('load',()=>script.remove());
    }

    //jsonp的回调函数
    function callback(obj) {
        var child=document.querySelector('.content').firstElementChild;
        if(child){
            child.remove();
        }
        creatContent(obj.s);
    }
    //创建li标签,即搜索提示栏
    function creatContent(arr) {
        let ul=document.createElement('ul');
        arr.forEach(
            (value)=>ul.appendChild(
                crElE('li',value,()=>search(value))
            )
        );
        document.querySelector('.content').appendChild(ul);
    }
    //创建元素,并给其添加文本内容及监听点击回调
    function crElE(nodeName,textContent,clickCallback) {
        let ele=document.createElement(nodeName);
        ele.textContent=textContent;
        ele.addEventListener('click',clickCallback);
        return ele;
    }
    //百度搜索
    function search(text) {
        window.location.href='https://www.baidu.com/s?tn=02049043_8_pg&ch=3&isource=infinity&wd='+text;
    }
</script>
</body>
</html>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值