vue實(shí)現(xiàn)百度搜索功能
本文實(shí)例為大家分享了vue實(shí)現(xiàn)百度搜索功能的具體代碼,供大家參考,具體內(nèi)容如下
最終效果:

Baidusearch.vue所有代碼:
<template>
<div>
<div class="container" id="app">
<div class="page-header">
<h2 class=" text-center text-primary">百度搜索案例</h2>
</div>
<form action="">
<div class=" form-group">
<input v-model="wd" @keyup="keyup($event)" @keydown="keydown($event)" type="text" class="form-control" />
<ul class="list-group">
<li class="list-group-item list-group-item-text" v-for="(item,index) in arr" :class="{'list-group-item-info':index==listIndex}" @click="click($event)">{{item}}</li>
</ul>
</div>
</form>
</div>
</div>
</template>
<script>
export default {
name: "Baidusearch",
data(){
return{
wd:'',//搜索的關(guān)鍵詞
arr:[],//用于儲(chǔ)存關(guān)鍵詞的搜索詞條
listIndex:-1//設(shè)置初始索引,數(shù)組從0開始,因此初始成-1
}
},
methods:{
//這個(gè)函數(shù)我們?cè)趇nput標(biāo)簽輸入關(guān)鍵詞的時(shí)候不斷的給百度服務(wù)器發(fā)送請(qǐng)求獲取搜索詞條,并且不斷的復(fù)制給data中的數(shù)組arr
keyup(event){
//如果我按的是上下鍵,那么就不發(fā)送請(qǐng)求了
console.log(event);
if(event.keyCode==38||event.keyCode==40||event.keyCode==13) return ;
var url="https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su"
this.$http.jsonp(url,{
params:{
wd:this.wd
},
jsonp:'cb'
}).then(res=>{
console.log(res);
this.arr=res.data.s;//把百度服務(wù)器返回的數(shù)據(jù)傳給arr數(shù)組
})
},
//監(jiān)聽鼠標(biāo)的點(diǎn)擊事件
//如果鼠標(biāo)點(diǎn)擊某一行的文字,則點(diǎn)擊事件中的event.explicitOriginalTarget.data代表關(guān)鍵詞
//如果點(diǎn)擊某一行的空白處,則點(diǎn)擊事件中的event.explicitOriginalTarget.innerText代表關(guān)鍵詞
//大家可以通過console.log(event)來查看關(guān)鍵詞所在的位置
click(event){
console.log(event);
if(event.explicitOriginalTarget.data!=undefined){
this.wd=event.explicitOriginalTarget.data;
window.open("https://www.baidu.com/s?wd="+this.wd);
}else if(event.explicitOriginalTarget.innerText!=undefined){
this.wd=event.explicitOriginalTarget.innerText;
window.open("https://www.baidu.com/s?wd="+this.wd);
}
},
//監(jiān)聽鍵盤的事件
//如果按down,則增加當(dāng)前l(fā)istIndex+1,因此arr[this.listIndex]就能代表當(dāng)前的詞條
//我們通過對(duì)listIndex的修改來得到當(dāng)前詞條在arr中的索引,然后就可以得到詞條的具體信息,然后發(fā)送請(qǐng)求了
keydown(event){
console.log(event);
//下鍵:40 上鍵:38
if(event.keyCode==38){
//按的上鍵
this.listIndex--;
if(this.listIndex<0){
this.listIndex=this.arr.length-1;
}
this.wd=this.arr[this.listIndex];
}
else if(event.keyCode==40){
//說明你按的是下鍵
this.listIndex++;
if(this.listIndex>this.arr.length-1){
this.listIndex=0;
}
this.wd=this.arr[this.listIndex];
}else if(event.keyCode==13){
//如果你按的是enter,那么就跳轉(zhuǎn)到百度搜索結(jié)果
window.open("https://www.baidu.com/s?wd="+this.wd);
}
}
}
}
</script>
<style scoped>
</style>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
在nuxt使用vueX代替storage的實(shí)現(xiàn)方案
這篇文章主要介紹了在nuxt使用vueX代替storage的實(shí)現(xiàn)方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
Vue項(xiàng)目分環(huán)境打包的實(shí)現(xiàn)步驟
這篇文章主要介紹了Vue項(xiàng)目如何分環(huán)境打包,實(shí)現(xiàn)方法大概分為六步驟,需要的朋友可以參考下2018-04-04
vue時(shí)間格式總結(jié)以及轉(zhuǎn)換方法詳解
項(xiàng)目中后臺(tái)返回的時(shí)間有多種形式,時(shí)間戳、ISO標(biāo)準(zhǔn)時(shí)間格式等,我們需要轉(zhuǎn)化展示成能看的懂得時(shí)間格式,下面這篇文章主要給大家介紹了關(guān)于vue時(shí)間格式總結(jié)以及轉(zhuǎn)換方法的相關(guān)資料,需要的朋友可以參考下2022-12-12
vue異步組件與組件懶加載問題(import不能導(dǎo)入變量字符串路徑)
這篇文章主要介紹了vue異步組件與組件懶加載問題(import不能導(dǎo)入變量字符串路徑),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
vue中img或元素背景圖片無法顯示或路徑錯(cuò)誤的解決
這篇文章主要介紹了vue中img或元素背景圖片無法顯示或路徑錯(cuò)誤的解決方案,具有很好的參考價(jià)值。希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
使用webpack打包后的vue項(xiàng)目如何正確運(yùn)行(express)
這篇文章主要介紹了使用webpack打包后的vue項(xiàng)目如何正確運(yùn)行(express) ,接下來通過本文給大家詳細(xì)介紹,需要的朋友可以參考下2018-10-10
vue單頁應(yīng)用加百度統(tǒng)計(jì)代碼(親測有效)
這篇文章主要介紹了vue單頁應(yīng)用加百度統(tǒng)計(jì)代碼的解決方法,需要的朋友參考下吧2018-01-01

