用vue的jsonp写了个百度搜索的demo


源代码可以到我的github上下载 https://github.com/fanhu371328/vue-

一:页面代码

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>vue百度搜索</title>
    
    <link rel="stylesheet" href="css/mobile.common.css"/>
    <link rel="stylesheet" href="css/main.css"/>
  </head>
  <body>
    <div id="app">
    		<h3 class="title">给我搜</h3>
    		
    		<!--展示部分-->
	    <div class="content">
	    		<div class="first">
	    			<div class="content_input clearfix">
				      <input type="text" class="search_field" @keyup="get($event)" @keydown.down.prevent="changeDown"  @keydown.up.prevent="changeUp" v-model='msg' placeholder="输入要搜索的内容">
				      <button class="search_btn" @click='open'>查询</button>     
				</div>
	    		</div>
	    		<ul class="list">
	    			<li v-for="item in messages" :class="{active:$index==now}" @mouseover="change($index)" @click='open'> {{item}} </li>
	    		</ul>
	    		<p v-show='messages.length==0'>暂无数据...</p>
	    </div>
	    
    </div>
  </body>
  <script src="./js/vue.js"></script>
  <script src="js/vue-resource.js"></script>
  <script src="js/index.js"></script>
</html>

二:样式

#app{width: 100%;height: 100%;}
.title {width: 100%;text-align: center;line-height: 50px;color: #fff;background: yellowgreen;}
.content {width: 639px;margin: 50px auto;position: relative;}
.content .first {width: 100%;font-size: 0;}
/*---------------------------------------------搜索框*/
.content_input .search_field{ /*输入框样式*/
    width: 536px;
    height: 38px;
    line-height: 38px;
    border: 1px solid #3388FF;
    float: left;
    padding-left: 10px;
    font-size: 18px;
    color: #666;
}
.content_input .search_btn{ /*按钮样式*/
    width: 103px;
    height: 38px;
    line-height: 38px;
    color: #FFFFFF;
    font-weight: 600;
    background: #3388FF;
    border:none;
    float: left;
    font-size: 20px;
}
/*===-----------------------------*/
.list {border: 1px solid #ccc;border-top: none;}
.list li{font-size: 18px;padding:5px 10px;}
.content>p {font-size: 18px;}
.active {background: #f0f0f0;}

三:js

(function(){
	//提交留言
	new Vue({
  		el:'#app',
  		data:{ //数据
  			msg:'',
  			msg1:'',
  			messages:[],
  			now:-1
  		},
  		methods:{ //方法 这里的this就是new的vue对象
  			get:function(e){
  				//保存输入框中输入的内容
  				this.msg1=this.msg;
  				if(e.keyCode == 38 || e.keyCode == 40){
  					return;
  				}
  				if(e.keyCode == 13){
  					window.open('https://www.baidu.com/s?wd='+this.msg);
  					this.msg='';
  					return;
  				}
				this.$http.jsonp('https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd='+ this.msg,{
					jsonp:'cb',
				}).then(function(data){
					this.messages=data.data.s;
				},function(){
//					console.log("erro");
				})
			},
			open:function(){
				window.open('https://www.baidu.com/s?wd='+this.msg);
				this.msg='';
			},
			changeDown:function(){
				this.now++;
				if(this.now >= this.messages.length){
					this.now = -1
				}
				this.msg=this.messages[this.now];
			},
			changeUp:function(){
				this.now--;
				if(this.now <= -2){
					this.now = this.messages.length -1 ;
				}
				this.msg=this.messages[this.now];
			},
			change:function(index){
				this.now=index;
				this.msg=this.messages[this.now];
			}
  		}
  	})
	
//百度搜索接口
//	https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=a&cb=jshow
//  https://www.baidu.com/s?wd=s
})()


  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老虎帅呆了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值