360搜索

jsonp跨域訪問接口
雖然js是受到同源策略,但是引用js文件,img,css文件是不會(huì)受到限制的。我們可以利用標(biāo)簽的src屬性這個(gè)特點(diǎn),動(dòng)態(tài)的創(chuàng)建script標(biāo)簽,通過src屬性發(fā)送相關(guān)的請(qǐng)求,請(qǐng)求的時(shí)候,返回回來的數(shù)據(jù),是一個(gè)函數(shù)調(diào)用,會(huì)被當(dāng)作JS代碼執(zhí)行,所以只要我們提前聲明好和返回的函數(shù)調(diào)用同名的函數(shù),那么在數(shù)據(jù)請(qǐng)求成功之后,

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
            list-style-type: none;
        }
        .box {
            width: 250px;
            height: 40px;
            border: 1px solid #ccc;
            border-radius: 5px;
            position: absolute;
            top: 100px;
            left: 50%;
            margin-left: -125px;
        }
        .box input {
            height: 100%;
            width: 100%;
            border: none;
            text-indent: 10px;
            outline: none;
        }
        .box ul {
            position: absolute;
            width: 100%;
            top: 40px;
            left: 0;
            border: 1px solid #ccc;
            border-top: none;
            display: none;
        }
        .box ul li {
            height: 40px;
            line-height: 40px;
            border-bottom: 1px solid #ccc;
            text-indent: 10px;
        }
    </style>
</head>
<body>
    <div class='box'>
        <input type="text" placeholder="請(qǐng)輸入搜索內(nèi)容" value="">
        <ul></ul>
    </div>

  <script type="text/javascript" src="art-template.js"></script>

  <script type="text/template" id="tpl">
      {{each result}}
      <li>
        <a  id="">{{$value.word}}</a>
      </li>
      {{/each}}
  </script>

  <script type="text/javascript">
      window.onload = function () {

                var input = document.querySelector('input');
        var ul = document.querySelector('ul');

        //將這個(gè)作為window的屬性,這樣的話在哪里都可以調(diào)用的到
        window.suggest_so = function (data) {
          ul.innerHTML = template('tpl',data);
        }

        input.onkeyup = function () {
          var val = this.value;
          //當(dāng)輸入的值為空的時(shí)候隱藏ul
          if(val == "") {
            ul.style.display = "none";
          } else {
            ul.style.display = "block";
          }
          //創(chuàng)建一個(gè)script標(biāo)簽,讓這個(gè)標(biāo)簽發(fā)出請(qǐng)求
          var script = document.createElement('script');
          script.src = 'https://sug.so.#/suggest?callback=suggest_so&encodein=utf-8&encodeout=utf-8&format=json&fields=word&word='+val;
          document.body.appendChild(script);
          document.body.removeChild(script);
        }
      }
  </script>

</body>
</html>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 五十三:請(qǐng)解釋 JavaScript 中 this 是如何工作的。1.方法調(diào)用模式當(dāng)一個(gè)函數(shù)被保存為一個(gè)對(duì)象的屬性...
    Arno_z閱讀 694評(píng)論 0 2
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,694評(píng)論 19 139
  • AJAX 原生js操作ajax 1.創(chuàng)建XMLHttpRequest對(duì)象 var xhr = new XMLHtt...
    碧玉含香閱讀 3,580評(píng)論 0 7
  • 這是朋友借給我的一本很好的書籍,很多涉及我剛剛考過的知識(shí)點(diǎn)。而且富有多種案例與經(jīng)典書籍的核心思想。粗粗翻過一遍,我...
    余師師閱讀 1,085評(píng)論 0 3
  • 如何避免職場(chǎng)沖突 文/寧國(guó)濤 人的精力和時(shí)間都是非常有限的,避免職場(chǎng)沖突,與同事相處得和諧,才能夠集中精力認(rèn)真工作...
    寧讓職場(chǎng)更給力閱讀 2,953評(píng)論 72 68

友情鏈接更多精彩內(nèi)容