jsonp實(shí)現(xiàn)百度下拉框功能的方法分析
本文實(shí)例講述了jsonp實(shí)現(xiàn)百度下拉框功能的方法。分享給大家供大家參考,具體如下:
思路就是獲取用戶輸入,然后根據(jù)用戶輸入調(diào)用百度的一個(gè)接口jsonp實(shí)現(xiàn)跨域請(qǐng)求,然后將百度返回給的內(nèi)容渲染數(shù)據(jù)到視圖。需要注意的就是,發(fā)送請(qǐng)求的時(shí)候記得編碼用戶輸入的內(nèi)容
var obj=document.querySelector('#user-input');
var body=document.querySelectorAll('body')[0];
var ul=document.querySelector('#ul');
var inner='';
function render(data){
//刪除前一次請(qǐng)求的li的內(nèi)容
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. 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>
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript操作DOM技巧總結(jié)》、《JavaScript頁面元素操作技巧總結(jié)》、《JavaScript事件相關(guān)操作與技巧大全》、《JavaScript查找算法技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
- jsonp跨域及實(shí)現(xiàn)百度首頁聯(lián)想功能的方法
- 百度搜索框智能提示案例jsonp
- 利用jsonp跨域調(diào)用百度js實(shí)現(xiàn)搜索框智能提示
- 使用jsonp完美解決跨域問題
- Jsonp post 跨域方案
- 跨域請(qǐng)求之jQuery的ajax jsonp的使用解惑
- JSONP跨域的原理解析及其實(shí)現(xiàn)介紹
- js實(shí)現(xiàn)跨域的幾種方法匯總(圖片ping、JSONP和CORS)
- jQuery使用JSONP實(shí)現(xiàn)跨域獲取數(shù)據(jù)的三種方法詳解
- AJAX實(shí)現(xiàn)跨域的三種方法(代理,JSONP,XHR2)
- AJax與Jsonp跨域訪問問題小結(jié)
相關(guān)文章
JS實(shí)現(xiàn)深度優(yōu)先搜索求解兩點(diǎn)間最短路徑
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)深度優(yōu)先搜索求解兩點(diǎn)間最短路徑,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
小程序中使用css var變量(使js可以動(dòng)態(tài)設(shè)置css樣式屬性)
這篇文章主要介紹了小程序中使用css var變量,使js可以動(dòng)態(tài)設(shè)置css樣式屬性,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
js利用for in循環(huán)獲取 一個(gè)對(duì)象的所有屬性以及值的實(shí)例
下面小編就為大家?guī)硪黄猨s利用for in循環(huán)獲取 一個(gè)對(duì)象的所有屬性以及值的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03
asp.net中System.Timers.Timer的使用方法
asp.net中System.Timers.Timer的使用方法,需要的朋友可以參考一下2013-03-03

