vue学习笔记

Vue百度搜索框实例

2019-11-11  本文已影响0人  果木山

百度搜索框实例

 <!DOCTYPE html>
 <html lang="en">
 <head>
     <meta charset="UTF-8">
     <title>百度搜索框</title>
     <link rel="stylesheet" href="css/bootstrap.css"/>
 </head>
 <body>
 <div class="container">
     <div id="app">
         <h1>百度搜索</h1>
         <form>
             <div class="form-group">
                 <input type="text" class="form-control" id="text" v-model="msg" @keyup="getData" @keydown.down="putDown" @keydown.up.prevent="putUp" @keydown.enter="goSearch"/>
             </div>
         </form>
         <ul class="list-group">
             <li v-for="(item,i) in ary" class="list-group-item" :class="{active:index===i}">{{item}}</li>
         </ul>
     </div>
 </div>
 <script src="js/vue.js"></script>
 <script src="js/vue-resource.js"></script>
 <script>
     new Vue({
         el:"#app",
         data:{
             msg:"",
             ary:[],
             index:0
         },
         methods:{
             getData(e){
                 //当按下键和上键时,阻止搜索功能;
                 if(e.keyCode===38 || e.keyCode===40) return;
                 //发送jsonp请求,跨域获取数据
                 if(this.msg!==""){
                     this.$http.jsonp("https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su",{
                         params:{
                             wd:this.msg
                         },
                         jsonp:"cb"
                     }).then(res=>{
                         this.ary=res.body.s;
                     })
                 }else{
                     this.ary=[];
                 }
             },
             putDown(){
                 //按下键,让对应的变亮,主要是改变this.index的值
                 this.index++;
                 this.index%=this.ary.length;
                 //更新文本框中的数据
                 this.msg=this.ary[this.index];
             },
             putUp(){
                 //按上键,让对应的变亮
                 this.index--;
                 if(this.index<0){
                     this.index=this.ary.length-1;
                 }
                 this.msg=this.ary[this.index];
             },
             goSearch(){
                 //按下回车键,跳转搜索
                 window.open(`https://www.baidu.com/s?wd=${this.msg}`,"_blank")
             }
         }
     })
 </script>
 </body>
 </html>

升级版百度搜索框实例

 <!DOCTYPE html>
 <html lang="zh-CN">
 <head>
     <meta charset="utf-8">
     <meta http-equiv="X-UA-Compatible" content="IE=edge">
     <meta name="viewport" content="width=device-width, initial-scale=1">
     <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
     <title>升级版百度搜索框实例</title>
 
     <!-- Bootstrap -->
     <link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
     <style>
         h1{
             margin: 20px 0 10px;
         }
         .input-group{
             width: 100%;
         }
         .input-group .form-control:last-child{
             border-top-left-radius: 6px;
             border-bottom-left-radius: 6px;
         }
         .input-group .form-control:first-child{
             border-top-right-radius: 6px;
             border-bottom-right-radius: 6px;
         }
         .list-group{
             margin: 15px 0;
         }
         .list-group .list-group-item{
             cursor: pointer;
         }
         .bgc{
             background-color: lightskyblue;
         }
     </style>
 
     <!-- HTML5 shim 和 Respond.js 是为了让 IE8 支持 HTML5 元素和媒体查询(media queries)功能 -->
     <!-- 警告:通过 file:// 协议(就是直接将 html 页面拖拽到浏览器中)访问页面时 Respond.js 不起作用 -->
     <!--[if lt IE 9]>
     <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
     <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
     <![endif]-->
 </head>
 <body>
 <div class="container">
     <div id="app">
         <h1>百度搜索框实例</h1>
         <div class="input-group">
             <input type="text" class="form-control" v-model="msg" @keyup="getData" @keydown.up.prevent="putUp" @keydown.down="putDown" @keydown.enter="goSearch"/>
         </div>
         <ul class="list-group">
             <li class="list-group-item" v-for="(item,index) in dataAry" :class="index===n-1?'bgc':''" :key="index" @mouseenter="mouseEnter(index)" @mouseleave="mouseLeave(index)" @click="goSearch(index,item)">{{item}}</li>
         </ul>
     </div>
 </div>
 <!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery,所以必须放在前边) -->
 <script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
 <!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
 <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>
 <!--引入vue.js-->
 <script src="./js/vue.js"></script>
 <!--引入vue-resource.js-->
 <script src="./js/vue-resource.js"></script>
 <script>
     new Vue({
         el: "#app",
         created(){
             if(this.msg=""){
                 this.dataAry=[];
             }
         },
         data: {
             initVal:this.msg,
             msg: "",
             n: 0,
             dataAry: []
         },
         methods: {
             getData(e){
                 //当按键为向上和向下时,无需获取新的数据
                 if(e.keyCode===38 || e.keyCode===40) return;
                 if(this.msg!==""){
                     this.initVal=this.msg;
                     this.n=0;
                     this.$http.jsonp("https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su",{
                         params: {
                             wd: this.msg
                         },
                         jsonp: "cb"
                     }).then(res => {
                         var ary=res.body.s;
                         if(ary.length>=6){
                             ary.splice(6);
                         }
                         this.dataAry=ary;
                     },err => {
                         console.log(err);
                     })
                 }else{
                     this.dataAry=[];
                 }
             },
             putUp(){
                 this.n--;
                 if(this.n===0){
                     //赋值为原来输入框内的数据
                     this.msg=this.initVal;
                     return;
                 }
                 if(this.n<0){
                     if(this.dataAry.length>0){
                         this.n=this.dataAry.length;
                     }
                 }
                 //将数组内容赋给文本输入框
                 this.msg=this.dataAry[this.n-1];
             },
             putDown(){
                 this.n++;
                 if(this.n>this.dataAry.length){
                     this.msg=this.initVal;
                     this.n=0;
                     return;
                 }
                 this.msg=this.dataAry[this.n-1];
             },
             mouseEnter(index){
                 this.n=index+1;
             },
             mouseLeave(){
                 //移出后,背景色消失;
                 this.n=0;
             },
             goSearch(e,item){
                 //知识点:@keydown.enter和@click事件;1)在绑定事件时,如果不传实参,则事件触发后默认向函数体中传入一个事件对象e;2)在绑定事件时,若给函数体传了实参值,则事件在触发后,不会再默认传入事件对象,若需要获取事件对象,则可在传入实参时,传入$event实参;
                 //筛选,判断第二个参数是否会传入,若传入了,则为点击事件触发,则需将文本输入框内的数据赋值为列表框内的数据,并跳转页面
                 if(item!==undefined){
                     //如果不等于undefined,则为click事件
                     this.msg=item;
                 }
                 //跳转新页面搜索
                 window.open("https://www.baidu.com/s?wd="+this.msg);
                 this.n=0;
             }
         }
     })
 </script>
 </body>
 </html>
上一篇 下一篇

猜你喜欢

热点阅读