欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue實(shí)現(xiàn)百度下拉提示搜索功能

 更新時(shí)間:2017年06月21日 16:38:55   作者:致Great  
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)百度下拉提示搜索功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

一、前期準(zhǔn)備

網(wǎng)上大神已經(jīng)做過這個(gè)功能https://github.com/lavyun/vue-demo-search 這自己僅實(shí)現(xiàn)搜索功能
為了使用百度實(shí)現(xiàn)搜索功能,首先搞清楚下拉數(shù)據(jù)和搜索功能的數(shù)據(jù)接口

01、提示數(shù)據(jù)獲取地址

打開百度官網(wǎng)打開開發(fā)者調(diào)試工具,選中network一項(xiàng),然后我們?cè)谒阉骺蜉斎?a',將會(huì)network發(fā)送的請(qǐng)求,這個(gè)就是提示數(shù)據(jù)的數(shù)據(jù)獲取地址


提示數(shù)據(jù)獲取地址.png

然后簡化一下:

復(fù)制代碼 代碼如下:
https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=a&cb=jQuery110208352732182923484_1497924041050&_=1497924041057#

其中“wd=”后接搜索的關(guān)鍵字,“cb=”后接回調(diào)函數(shù)


輸入a時(shí),請(qǐng)求的提示數(shù)據(jù)

02:搜索功能實(shí)現(xiàn)地址

在輸入框中輸入“a”之后,點(diǎn)擊搜索按鈕之后,地址欄中地址就是實(shí)現(xiàn)搜索功能的地址


搜索地址.png

搜索地址簡化前后對(duì)比,是不是看起來很舒服了O(∩_∩)O


簡化地址.png

我們使用簡化之后的地址,搜索關(guān)鍵字“s‘'測(cè)試一下


測(cè)試.png

二、代碼實(shí)現(xiàn)

js代碼

 new Vue({
    el:'#app',
    data:{
      myData:[],
      keyword:'',
      now:-1
    },
    methods:{
      get:function (event) {
        if(event.keyCode==38||event.keyCode==40)return;
        if(event.keyCode==13){
          window.open('https://www.baidu.com/s?wd='+this.keyword);
          this.keyword=''
        }
        this.$http.jsonp('https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su',{
          wd:this.keyword
        },{
          jsonp:'cb'
        }).then(function (res) {
          this.myData=res.data.s;
        },function () {

        });
      },
      selectDown:function () {
        this.now++;
        if(this.now==this.myData.length)this.now=-1;
        this.keyword=this.myData[this.now];
      },
      selectUp:function () {
        this.now--;
        if(this.now==-2)this.now=this.myData.length-1;
        this.keyword=this.myData[this.now];
      }
    }
  })

html代碼

<div class="container search-container" id="app">
  <h1 class="title" >baidu-search</h1>
  <input type="text" class="form-control" placeholder="請(qǐng)輸入想要搜索關(guān)鍵字" v-model="keyword" @keyup="get($event)" @keydown.down.prevent="selectDown"
  @keydown.up.prevent="selectUp">
  <ul>
    <li class="text-center" v-for="(value,index) in myData"><span class="text-success textprimary" :class="{gray:index==now}">{{value}}</span></li>
  </ul>
  <p ><h2 v-show="myData.length==0" class="text-warning text-center">(*^__^*)暫時(shí)沒有數(shù)據(jù)</h2></p>
</div>

get方法實(shí)現(xiàn)獲取下拉數(shù)據(jù)和搜索功能,輸入keyword之后,調(diào)用get方法使用jsonp獲取提示數(shù)據(jù),然后賦值給myData,然后使用v-for遍歷提示數(shù)據(jù)


提示數(shù)據(jù).png

然后selectDown和selectUp實(shí)現(xiàn)上下選中數(shù)據(jù),當(dāng)按下回車鍵時(shí),實(shí)現(xiàn)搜索
完整代碼:https://github.com/yanqiangmiffy/baidu-search

三、實(shí)現(xiàn)效果


以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論