国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

使用Bootrap和Vue實現仿百度搜索功能

2019-11-19 15:04:23
字體:
來源:轉載
供稿:網友

用Vue調用百度的搜索接口,實現簡單的搜索功能。

搜索框的樣式是基于Bootstrap,當然對樣式做了簡單的調整, 使之類似于百度搜索。代碼如下

<!DOCTYPE html><html><head>  <meta charset="utf-8">  <title>百度搜索</title>  <style type="text/css">    .gray{      background-color: #eee;    }    .listyle{      font-size: 16px;      line-height: 35px;      padding-left: 16px;    }    .ulstyle{      border:1px solid #ccc;      border-top: none;    }  </style>  <link rel="stylesheet" type="text/css" href="bootstrap.min.css" rel="external nofollow" >  <script type="text/javascript" src="vue.js"></script>  <script type="text/javascript" src="vue-resource.js"></script>  <script type="text/javascript">    window.onload = function(){      new Vue({        el: ".container",        data: {          myData:[],          txt:"",          nowIndex:-1        },        methods:{          get:function(event){            if(event.keyCode==38 || event.keyCode==40){              return;            }            if(event.keyCode==13){              window.open("https://www.baidu.com/s?wd="+this.txt);              this.txt="";            }            this.$http.jsonp("https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su",{              wd:this.txt            },{              jsonp:"cb"            }).then(function(res){              this.myData=res.data.s            },function(res){              alert(res.status);            });          },          changeDown:function(){            this.nowIndex++;            if(this.nowIndex==this.myData.length){              this.nowIndex=0;              this.txt=this.myData[0];            }else{              this.txt=this.myData[this.nowIndex];            }          },          changeUp:function(){            this.nowIndex--;            if(this.nowIndex==-1){              this.nowIndex=this.myData.length-1;              this.txt=this.myData[this.nowIndex];            }else{              this.txt=this.myData[this.nowIndex];            }          },          mouseOver:function(n){            this.nowIndex=n;            this.txt=this.myData[this.nowIndex];          },          getMsg:function(){            window.open("https://www.baidu.com/s?wd="+this.txt);            this.txt="";          }        }      });    }  </script></head><body>  <br>  <div class="container">    <div class="input-group">      <input type="text" class="form-control input-lg" placeholder="請輸入關鍵字" v-model="txt" @keyup="get($event)" @keydown.down="changeDown()" @keydown.up="changeUp()">      <span class="input-group-btn">        <button class="btn btn-default btn-lg" type="button" @click="getMsg()">搜索</button>      </span>    </div>    <ul class="list-unstyled ulstyle" v-show="myData.length!=0">      <li v-for="item in myData" :class={gray:$index==nowIndex,listyle:true} @mouseover="mouseOver($index)" @click="getMsg()">{{item}}</li>    </ul>  </div></body></html> 

實現效果如下

這里寫圖片描述

這里寫圖片描述

總結

以上所述是小編給大家介紹的使用Bootrap和Vue實現仿百度搜索功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 肃宁县| 卫辉市| 黄大仙区| 临桂县| 海原县| 游戏| 容城县| 鹤岗市| 清水县| 漳浦县| 三穗县| 新河县| 江达县| 临西县| 天镇县| 鹿邑县| 莲花县| 宜兰市| 手机| 海南省| 怀集县| 高淳县| 定西市| 东平县| 邓州市| 偏关县| 成武县| 荥经县| 游戏| 兴城市| 兴化市| 桐柏县| 肇东市| 海盐县| 维西| 鹤峰县| 专栏| 游戏| 曲周县| 长丰县| 开化县|