国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > JS > 正文

jsonp實現百度下拉框功能的方法分析

2024-05-06 16:50:42
字體:
來源:轉載
供稿:網友

本文實例講述了jsonp實現百度下拉框功能的方法。分享給大家供大家參考,具體如下:

思路就是獲取用戶輸入,然后根據用戶輸入調用百度的一個接口jsonp實現跨域請求,然后將百度返回給的內容渲染數據到視圖。需要注意的就是,發送請求的時候記得編碼用戶輸入的內容

var obj=document.querySelector('#user-input');var body=document.querySelectorAll('body')[0];var ul=document.querySelector('#ul');var inner='';function render(data){    //刪除前一次請求的li的內容    if(ul.innerHTML!=''){      ul.innerHTML='';    }    for(let i = 0, length1 = data.s.length; i < length1; i++){      var li=document.createElement('li');      li.innerHTML=data.s[i];      ul.appendChild(li);    }}obj.addEventListener('keyup',function(){    if(document.querySelector('#request')){      body.removeChild(document.querySelector('#request'));    }      var script=document.createElement('script');      script.id="request";      script.src="http://unionsug.baidu.com/su?wd="+encodeURI(obj.value.trim())+'&p=3&cb=render';      body.appendChild(script);});//利用冒泡添加事件。ul.addEventListener('click',function(e){    var e=e||window.event;    window.location.href="https://www.baidu.com/s?word=" rel="external nofollow" +encodeURI(e.target.innerHTML);});
<style type="text/css">  *{          margin: 0;          padding: 0;  }  ul{          margin-left: 10px;          transition: all 1s ease;  }    input{          width: 300px;          height: 40px;          line-height: 40px;          background: #4caf50a6;          outline: none;          border: none;          border-radius: 10px;          padding-left: 15px;          color: white;          font-size: 20px;    }    li{          cursor: pointer;          transition: all 1s ease;          list-style: none;          width: 280px;          height: 30px;          line-height: 30px;          background: #8acb8da8;          color: #888e4a;          padding-left: 10px;    }    li:hover{          background: #64a968;          color: #caf1cc;    }    input::-webkit-input-placeholder{      color:white;    }    input::-moz-placeholder{  /* Mozilla Firefox 19+ */      color:white;    }    input:-moz-placeholder{  /* Mozilla Firefox 4 to 18 */      color:white;    }    input:-ms-input-placeholder{ /* Internet Explorer 10-11 */       color:white;    }</style>

希望本文所述對大家JavaScript程序設計有所幫助。


注:相關教程知識閱讀請移步到JavaScript/Ajax教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 贡嘎县| 安国市| 顺平县| 自贡市| 郁南县| 平乐县| 中山市| 兴城市| 松潘县| 莱芜市| 古蔺县| 广平县| 满城县| 温泉县| 玛沁县| 兴隆县| 玛多县| 游戏| 祁连县| 北海市| 和顺县| 九江市| 翼城县| 屏山县| 井冈山市| 芮城县| 松江区| 甘谷县| 四子王旗| 乌拉特后旗| 阿尔山市| 基隆市| 河东区| 勐海县| 桑植县| 永泰县| 比如县| 乌兰察布市| 屯门区| 五家渠市| 怀仁县|