类别:企业动态 发布时间:2021-01-12 浏览人次:
这段代码用到vuejs和vue-resouece。实现对接智能提示接口,并通过上下键选择提示项,按enter进行搜索
!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('s wd=' + this.tt); this.tt = ''; this.$http.jsonp('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()" li v-for="(item, index) in myData" : {{item}} /li /ul /div /body /html
效果图:
这个ajax请求没有做节流,很多时候需要限制ajax频繁请求,可以小改一下:
!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('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('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()" li v-for="(item, index) in myData" : {{item}} /li /ul /div /body /html
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持凡科。
vue完成百度搜索检索往下拉提醒作用案例 创作者: 本文关键详细介绍了vue完成百度搜索检索往下拉提醒作用案例,具备一定的参照使用价值,很感兴趣的小伙子伴们能够参照一下这...
2021-01-12招聘人数:24职位信息1、年龄20一36岁;2、有白酒或啤酒工作经验或退伍军人优先;3、3500元底薪加提成;4、工作时间:下午14:00~21:00;5、工作地点:天河、黄埔、海珠6、...
2021-01-12招聘人数:19职位信息1. 年轻少女心,美妆狂热分子,对“买买买”充满热情,对网红畅销品了如指掌,尤其擅长安利闺蜜,属于行走的"人肉种草机";2. 玩转各大电商平台(天猫、京东...
2021-01-12招聘人数:15职位信息【岗位职责】1、熟悉劳动法等相关法律法规,以及国家和地区关于薪酬制度、保险福利待遇等政策方针;2、建设与完善公司绩效及薪酬管理体系,梳理、优化绩效...
2021-01-12招聘人数:24职位信息工作内容:1.协助运营组推广公司的品牌、产品和活动,评估微信活动推广效果,并反馈给运营;2.对微信号的日常经营,及时更新、维护微信内容,发展粉丝规...
2021-01-12【鹊起高新科技_做提升完全免费建设网站_手机微信:179900】十多年随州技术专业网站建设企业,融合网站seo编码,有利于搜索引擎排名提升,随州网站建设企业哪个好?找鹊起高新科技!鹊起...
2021-01-12