欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Vue 仿百度搜索功能实现代码

程序员文章站 2022-10-06 18:43:51
无上下选择

无上下选择

<!doctype html>
<html lang="en">
<head>
 <meta charset="utf-8">
 <title>jsonp</title>
 <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
 <meta name="apple-mobile-web-app-capable" content="yes">
 <meta name="apple-mobile-web-app-status-bar-style" content="black">
 <script src="../js/vue.js"></script>
 <script src="../js/vue-resource.js"></script>
 <script>
  window.onload = function(){
   var vm = new vue({
    el:'#box',
    data:{
     mydata:[],
     t1:''
    },
    methods:{
     get:function(){
      this.$http.jsonp('https://sp0.baidu.com/5a1fazu8aa54nxgko9wtanf6hhy/su',{
       wd:this.t1
      },{
       jsonp:'cb'
      }).then(function(res){
       this.mydata=res.data.s
      },function(){
      });
     }
    }
   });
  }
 </script>
</head>
<body>
<div id="box">
 <input type="text" v-model="t1" @keyup="get()">
 <ul>
  <li v-for="value in mydata">{{value}}</li>
 </ul>
 <p v-show="mydata.length==0">暂无数据...</p>
</div>
</body>
</html>

加上上下选择

<!doctype html>
<html lang="en">
<head>
 <meta charset="utf-8">
 <title>jsonp</title>
 <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
 <meta name="apple-mobile-web-app-capable" content="yes">
 <meta name="apple-mobile-web-app-status-bar-style" content="black">
 <style>
  .gray{
   background: #ccc;
  }
 </style>
 <script src="../js/vue1.0.js"></script>
 <script src="../js/vue-resource.js"></script>
 <script>
  window.onload=function(){
   new vue({
    el:'body',
    data:{
     mydata:[],
     t1:'',
     now:-1
    },
    methods:{
     get:function(ev){
      if(ev.keycode==38||ev.keycode==40){
       return;
      }
      if(ev.keycode==13){
       window.open('https://www.baidu.com/s?wd='+this.t1);
       this.t1='';
      }
      this.$http.jsonp('https://sp0.baidu.com/5a1fazu8aa54nxgko9wtanf6hhy/su',{
       wd:this.t1
      },{
       jsonp:'cb'
      }).then(function(res){
       this.mydata=res.data.s;
      },function(){
      });
     },
     changedown:function(){
      this.now++;
      if(this.now==this.mydata.length)this.now=-1;
      this.t1=this.mydata[this.now];
     },
     changeup:function(){
      this.now--;
      if(this.now==-2)this.now=this.mydata.length-1;
      this.t1=this.mydata[this.now];
     }
    }
   });
  };
 </script>
</head>
<body>
<div id="box">
 <input type="text" v-model="t1" @keyup="get($event)" @keydown.down="changedown()" @keydown.up.prevent="changeup()">
 <ul>
  <li v-for="value in mydata" :class="{gray:$index==now}">{{value}}</li>
 </ul>
 <p v-show="mydata.length==0">暂无数据...</p>
</div>
</body>
</html>

以上所述是小编给大家介绍的vue 仿百度搜索功能实现代码,希望对大家有所帮助