jsonp跨域访问案例

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        form{
            width: 500px;
            border: 1px solid #f89406;
            margin: 0 auto;
            padding: 10px;
        }
        input{
            width: 100%;
            height:30px;
        }
        #content{
            border:1px solid #f90; width: 100%; margin: 0;padding: 0; display: none;list-style-type: none;
        }
        #content li a {
            line-height: 30px; padding: 5px; text-decoration: none; color: black; display: block;
        }
        #content li a:hover {
            background: #f90;
            color: white;
        }
    </style>
</head>
<body>

<form action="" method="get">
    <input type="text" id="search">
    <ul id="content"></ul>
</form>

</body>
</html>

<script>

    var search = document.getElementById('search');
    var content = document.getElementById('content');

    search.onkeyup = function () {
        if (null !== this.value){
            //创建script标签
            var oScript = document.createElement('script');
            oScript.src= 'http://suggestion.baidu.com/su?wd='+this.value+'&cb=supeMapJson';
            document.body.appendChild(oScript);
            content.style.display = 'display';
        }else {
            content.style.display = 'none';
        }
    }

    function supeMapJson(data) {
        data.p = true;
        var html = '';
        if (data.s.length){
            for (var i =0;i < data.s.length; i++){
                html += '<li><a href="http://www.baidu.com/s?wd='+encodeURI(data.s[i])+'">'+data.s[i]+'</a></li>';
            }
            content.innerHTML = html;
            content.style.display = 'block';
        }else {
            content.style.display = 'none';
        }
    }
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值