jsonp的使用

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <button id="btn">获取资源</button>
  11. </body>
  12. <script src="./untils.js"></script>
  13. <script>
  14. var oBtn = document.getElementById('btn');
  15. oBtn.onclick = function () {
  16. xhr.ajax({
  17. url: 'http:://test.jsplusplus.com/jsonp/jsonp.php',
  18. type: 'get',
  19. dataType: 'JSONP',
  20. jsonp: 'cb',
  21. //jsonpCallback: 'test',
  22. success: function (data) {
  23. console.log(data);
  24. }
  25. })
  26. }
  27. </script>
  28. </html>

image.png

实战百度联想词

image.png
实战百度联想词

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>百度联想词</title>
  6. <link rel="stylesheet" href="css/index.css" />
  7. </head>
  8. <body>
  9. <div class="search-wrap">
  10. <div class="input-wrap">
  11. <input type="text" class="search-input J_searchInput" />
  12. </div>
  13. <div class="list-wrap">
  14. <ul class="wd-list J_wdList"></ul>
  15. </div>
  16. </div>
  17. <script type="text/html" id="J_listTpl">
  18. <li class="wd-item">
  19. <a href="https://www.baidu.com/s?wd={{wdLink}}" target="_blank" class="wd-lk">{{wd}}</a>
  20. </li>
  21. </script>
  22. <script src="js/index.js"></script>
  23. </body>
  24. </html>
div, input, li{
    box-sizing: border-box;
    outline: none;
}

ul{
    padding: 0;
    margin: 0;
    list-style: none;
}

a{
    text-decoration: none;
}

.search-wrap{
    position: relative;
    width: 536px;
    height: 40px;
    margin: 200px auto;    
}

.search-wrap .search-input{
    width: 100%;
    height: 40px;
    border: 1px solid #b8b8b8;
    border-bottom: 1px solid #ccc;
    text-indent: 10px;
    font-size: 18px;
}

.search-wrap .search-input:hover{
    border: 1px solid #999;
}

.search-wrap .search-input:focus{
    border: 1px solid #38f;
}

.search-wrap .list-wrap{
    display: none;
    position: absolute;
    top: 40px;
    left: 0;
    width: 100%;
    border: 1px solid #ccc;
    background-color: #fff;
}

.search-wrap .list-wrap .wd-item{
    height: 25px;
    padding-left: 10px;
    line-height: 25px;
}

.search-wrap .list-wrap .wd-item:hover{
    background-color: #efefef;
}

.search-wrap .list-wrap .wd-lk{
    font-weight: bold;
    color: #000;
    font-size: 14px;
}

.search-wrap .list-wrap .wd-lk .font-normal{
    font-weight: normal;
}
;
(function (doc) {
    var searchInput = doc.getElementsByClassName('J_searchInput')[0],
        wdList = doc.getElementsByClassName('J_wdList')[0],
        listWrap = wdList.parentNode,
        firstScript = doc.getElementsByTagName('script')[0],
        listTpl = doc.getElementById('J_listTpl').innerHTML;

    var init = function () {
        bindEvent();
    }

    function bindEvent() {
        searchInput.addEventListener('input', typeInput, false);
    }

    function renderList(data) {

        var list = '',
            data = data.s,
            len = data.length,
            val = _trimSpace(searchInput.value);
        if (len) {
            data.forEach(function (elem) {
                list += listTpl.replace(/{{(.*?)}}/g, function (node, key) {
                    return {
                        wdLink: elem,
                        wd: _setWdStyle(val, elem)
                    } [key];
                });
            });
            wdList.innerHTML = list;
            listWrap.style.display = 'block';
        } else {
            wdList.innerHTML = '';
            listWrap.style.display = 'none';
        }
    }

    function typeInput() {
        // 去除输入数据的空格
        var val = _trimSpace(searchInput.value);
        if (val.length > 0) {
            // getDatas(val,'setDatas');
            getDatas(val);
        } else {
            wdList.innerHTML = '';
            listWrap.style.display = 'none';
        }
    }

    function getDatas(value) {
        // var oScript = doc.createElement('script');
        // oScript.src = 'https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=' + value + '&cb=' + callbackName;
        // doc.body.insertBefore(oScript, firstScript);
        // doc.body.removeChild(oScript);
        xhr.ajax({
            url: 'https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=' + value,
            type: 'GET',
            dataType: 'JSONP',
            jsonp: 'cb',
            success: function (data) {
                // console.log(data);
                renderList(data);
            }
        });
    }
    // 去除输入数据的空格
    function _trimSpace(str) {
        return str.replace(/\s+/g, '');
    }

    function _setWdStyle(value, word) {
        // 把输入的的字变细                                 //去除输入的字
        return '<span class="font-normal">' + value + '</span>' + word.replace(value, '');
    }

    // window.setDatas = function (data) {
    //     console.log(data);
    //     renderList(data);
    // }
    init();
})(document);