当前位置: 移动技术网 > IT编程>脚本编程>vue.js > vue实现百度下拉列表交互操作示例

vue实现百度下拉列表交互操作示例

2019年05月24日  | 移动技术网IT编程  | 我要评论

交通手抄报,七乐康大药房旗舰店,人体艺术林志玲

本文实例讲述了vue实现百度下拉列表交互操作。分享给大家供大家参考,具体如下:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>www.jb51.net vue百度下拉列表</title>
  <style>
    .gray{
      background: #ccc;
    }
  </style>
  <script src="vue.js"></script>
  <script src="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()">
    <!--搜索框的光标会默认定位到文字前面,这里@keydown.up.prevent阻止默认事件-->
    <ul>
      <li v-for="value in mydata" :class="{gray:$index==now}">
        {{value}}
      </li>
    </ul>
    <p v-show="mydata.length==0">暂无数据...</p>
  </div>
</body>
</html>

感兴趣的朋友可以使用在线html/css/javascript代码运行工具:测试上述代码运行效果。

希望本文所述对大家vue.js程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网