vue实现百度搜索下拉提示功能实例
内容摘要
这篇文章主要为大家详细介绍了vue实现百度搜索下拉提示功能实例,具有一定的参考价值,可以用来参考一下。
对此感兴趣的朋友,看看idc笔记做的技术笔记!
这段代码用到vuejs和vue-
对此感兴趣的朋友,看看idc笔记做的技术笔记!
这段代码用到vuejs和vue-
文章正文
这篇文章主要为大家详细介绍了vue实现百度搜索下拉提示功能实例,具有一定的参考价值,可以用来参考一下。
对此感兴趣的朋友,看看idc笔记做的技术笔记!
这段代码用到vuejs和vue-resouece。实现对接智能提示接口,并通过上下键选择提示项,按enter进行搜索
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | <code> <!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) { // 请求限制 按了上下箭头 if (e.keyCode === 38 || e.keyCode === 40) { return } // enter跳转 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) { // 请求成功 this.myData = res.data.s; this.now = -1; }, function (res) { // 请求失败 console.log(res.status) }) }, changeDown: function () { this.now++; // 到了最后一个选项 if (this.now === this.myData.length) { this.now = -1; } this.tt = this.myData[this.now] }, changeUp: function () { this.now--; // 到了第一个选项 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> </code> |
效果图:
【图片暂缺】
这个ajax请求没有做节流,很多时候需要限制ajax频繁请求,可以小改一下:
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | <code> <!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) { // 请求限制 按了上下箭头 if (e.keyCode === 38 || e.keyCode === 40) { return } // enter跳转 if (e.keyCode === 13) { window.open( 'https://www.baidu.com/s?wd=' + this.tt); this.tt = '' ; } // 限制频繁请求 this.throttle(this.getData,window) }, changeDown: function () { this.now++; // 到了最后一个选项 if (this.now === this.myData.length) { this.now = -1; } this.tt = this.myData[this.now] }, changeUp: function () { this.now--; // 到了第一个选项 if (this.now === -2) { this.now = this.myData.length - 1; } this.tt = this.myData[this.now] }, // 把请求单独拿出来 getData() { this. $http .jsonp( 'https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su' , { wd: this.tt }, { jsonp: 'cb' }).then( function (res) { // 请求成功 this.myData = res.data.s; this.now = -1; }, function (res) { // 请求失败 console.log(res.status) }) }, // 节流函数 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> </code> |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持php教程。
注:关于vue实现百度搜索下拉提示功能实例的内容就先介绍到这里,更多相关文章的可以留意
代码注释