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

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

 更新時(shí)間:2017年06月14日 11:30:11   作者:22337383  
這篇文章主要介紹了vue實(shí)現(xiàn)百度搜索下拉提示功能實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

這段代碼用到vuejs和vue-resouece。實(shí)現(xiàn)對(duì)接智能提示接口,并通過上下鍵選擇提示項(xiàng),按enter進(jìn)行搜索

<!DOCTYPE html>
<html lang="en">

<head>
 <meta charset="UTF-8">
 <title>Document</title>
 <script type="text/javascript" src="vue.js"></script>
 <script type="text/javascript" src="vue-resource.js"></script>
 <script type="text/javascript">
 window.onload = function() {
  var app = new Vue({
   el: '#box',
   data: {
    myData: [],
    tt: '',
    now: -1
   },
   methods: {
    get: function(e) {

     // 請(qǐng)求限制 按了上下箭頭
     if (e.keyCode === 38 || e.keyCode === 40) {
      return
     }
     // enter跳轉(zhuǎn)
     if (e.keyCode === 13) {
      window.open('https://www.baidu.com/s?wd=' + this.tt);
      this.tt = '';
     }
     this.$http.jsonp('https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su', {
      wd: this.tt
     }, {
      jsonp: 'cb'
     }).then(function(res) {
      // 請(qǐng)求成功
      this.myData = res.data.s;
        this.now = -1;
     }, function(res) {
      // 請(qǐng)求失敗
      console.log(res.status)
     })
    },
    changeDown: function() {
     this.now++;
     // 到了最后一個(gè)選項(xiàng)
     if (this.now === this.myData.length) {
      this.now = -1;
     }
     this.tt = this.myData[this.now]
    },
    changeUp: function() {
     this.now--;
     // 到了第一個(gè)選項(xiàng)
     if (this.now === -2) {
      this.now = this.myData.length - 1;
     }
     this.tt = this.myData[this.now]

    }
   }
  })
 }
 </script>
 <style type="text/css">
 .gray {
  background: gray
 }
 </style>
</head>

<body>
 <!-- 百度下拉接口 -->
 <div id="box">
  <input type="text" v-model="tt" name="" @keyup="get($event)" @keydown.down="changeDown()" @keydown.up="changeUp()">
  <ul>
   <li v-for="(item, index) in myData" :class="{gray:index===now}">{{item}}</li>
  </ul>
 </div>
</body>

</html>

效果圖:

這個(gè)ajax請(qǐng)求沒有做節(jié)流,很多時(shí)候需要限制ajax頻繁請(qǐng)求,可以小改一下:

<!DOCTYPE html>
<html lang="en">

<head>
 <meta charset="UTF-8">
 <title>Document</title>
 <script type="text/javascript" src="vue.js"></script>
 <script type="text/javascript" src="vue-resource.js"></script>
 <script type="text/javascript">
 window.onload = function() {
  var app = new Vue({
   el: '#box',
   data: {
    myData: [],
    tt: '',
    now: -1
   },
   methods: {
    get: function(e) {

     // 請(qǐng)求限制 按了上下箭頭
     if (e.keyCode === 38 || e.keyCode === 40) {
      return
     }
     // enter跳轉(zhuǎn)
     if (e.keyCode === 13) {
      window.open('https://www.baidu.com/s?wd=' + this.tt);
      this.tt = '';
     }
     // 限制頻繁請(qǐng)求
     this.throttle(this.getData,window)
    },
    changeDown: function() {
     this.now++;
     // 到了最后一個(gè)選項(xiàng)
     if (this.now === this.myData.length) {
      this.now = -1;
     }
     this.tt = this.myData[this.now]
    },
    changeUp: function() {
     this.now--;
     // 到了第一個(gè)選項(xiàng)
     if (this.now === -2) {
      this.now = this.myData.length - 1;
     }
     this.tt = this.myData[this.now]

    },
    // 把請(qǐng)求單獨(dú)拿出來(lái)
    getData() {
     this.$http.jsonp('https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su', {
      wd: this.tt
     }, {
      jsonp: 'cb'
     }).then(function(res) {
      // 請(qǐng)求成功
      this.myData = res.data.s;
        this.now = -1;
     }, function(res) {
      // 請(qǐng)求失敗
      console.log(res.status)
     })
    },
    // 節(jié)流函數(shù)
    throttle(method,context){
      clearTimeout(method.tId);
      method.tId=setTimeout(function(){
        method.call(context);
      },300);
    }
   }
  })
 }
 </script>
 <style type="text/css">
 .gray {
  background: gray
 }
 </style>
</head>

<body>
 <!-- 百度下拉接口 -->
 <div id="box">
  <input type="text" v-model="tt" name="" @keyup="get($event)" @keydown.down="changeDown()" @keydown.up="changeUp()">
  <ul>
   <li v-for="(item, index) in myData" :class="{gray:index===now}">{{item}}</li>
  </ul>
 </div>
</body>

</html>

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

相關(guān)文章

  • Vue.js實(shí)現(xiàn)音樂播放器

    Vue.js實(shí)現(xiàn)音樂播放器

    這篇文章主要為大家詳細(xì)介紹了Vue.js實(shí)現(xiàn)音樂播放器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • vue使用高德地圖根據(jù)坐標(biāo)定位點(diǎn)的實(shí)現(xiàn)代碼

    vue使用高德地圖根據(jù)坐標(biāo)定位點(diǎn)的實(shí)現(xiàn)代碼

    這篇文章主要介紹了vue使用高德地圖根據(jù)坐標(biāo)定位點(diǎn)的實(shí)現(xiàn)代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-08-08
  • 在Vue中使用Avue、配置過程及實(shí)際應(yīng)用小結(jié)

    在Vue中使用Avue、配置過程及實(shí)際應(yīng)用小結(jié)

    在項(xiàng)目中遇到通過點(diǎn)擊加號(hào)實(shí)現(xiàn)輸入框的增加、以及對(duì)該輸入框的輸入內(nèi)容進(jìn)行驗(yàn)證,通過這些誘導(dǎo)因素創(chuàng)作的這篇文章,本文重點(diǎn)給大家介紹在Vue中使用Avue、配置過程以及實(shí)際應(yīng)用,需要的朋友可以參考下
    2022-10-10
  • 淺析Vue 和微信小程序的區(qū)別、比較

    淺析Vue 和微信小程序的區(qū)別、比較

    寫了vue項(xiàng)目和小程序,發(fā)現(xiàn)二者有許多相同之處,在此想總結(jié)一下二者的共同點(diǎn)和區(qū)別,需要的朋友可以參考下
    2018-08-08
  • vue 實(shí)現(xiàn) ios 原生picker 效果及實(shí)現(xiàn)思路解析

    vue 實(shí)現(xiàn) ios 原生picker 效果及實(shí)現(xiàn)思路解析

    這篇文章主要介紹了vue 實(shí)現(xiàn) ios 原生picker 效果及實(shí)現(xiàn)思路解析,本文給大家介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-12-12
  • vue實(shí)現(xiàn)百度語(yǔ)音合成的實(shí)例講解

    vue實(shí)現(xiàn)百度語(yǔ)音合成的實(shí)例講解

    在本篇文章里小編給大家整理的是關(guān)于vue實(shí)現(xiàn)百度語(yǔ)音合成的實(shí)例內(nèi)容,以及相關(guān)代碼,需要的朋友們參考下。
    2019-10-10
  • vue3組件式彈窗打開的3種方式小結(jié)

    vue3組件式彈窗打開的3種方式小結(jié)

    這篇文章主要給大家介紹了關(guān)于vue3組件式彈窗打開的3種方式,彈窗組件是常見的交互組件,它能夠彈出一些提示信息、提醒用戶進(jìn)行操作等等,需要的朋友可以參考下
    2023-07-07
  • 關(guān)于vue3中的reactive賦值問題

    關(guān)于vue3中的reactive賦值問題

    這篇文章主要介紹了關(guān)于vue3中的reactive賦值問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • Vue3 appear 失效的問題及如何使用 appear

    Vue3 appear 失效的問題及如何使用 appear

    appear 是一個(gè)在元素默認(rèn)被顯示的情況下 調(diào)用進(jìn)入動(dòng)畫效果,使得元素在這種初次渲染情況下 執(zhí)行進(jìn)入動(dòng)畫的屬性,最近在學(xué)習(xí)vue3的動(dòng)畫時(shí)遇到appear無(wú)法生效的問題,本文給大家詳細(xì)講解,感興趣的朋友一起看看吧
    2023-10-10
  • vue3系統(tǒng)進(jìn)入頁(yè)面前的權(quán)限判斷和重定向方式

    vue3系統(tǒng)進(jìn)入頁(yè)面前的權(quán)限判斷和重定向方式

    這篇文章主要介紹了vue3系統(tǒng)進(jìn)入頁(yè)面前的權(quán)限判斷和重定向方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03

最新評(píng)論