<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.gray{
background: #ccc
}
</style>
</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==nowIndex}">
{{value}}
</li>
</ul>
<p v-show="myData.length==0">暂无数据...</p>
</div>
<script src="vue.js"></script>
<script src="vue-resource.js"></script>
<script>
new Vue({
el:"#box",
data:{
myData:[],
t1:'',
nowIndex:-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;
console.log(res.data);
},function(err){
console.log(err)
})
},
changeDown:function(){
this.nowIndex++;
if(this.nowIndex==this.myData.length){
this.nowIndex=-1;
}
this.t1=this.myData[this.nowIndex]
},
changeUp:function(){
this.nowIndex--;
if(this.nowIndex==-2){
this.nowIndex=this.myData.length-1;
}
this.t1=this.myData[this.nowIndex]
}
}
})
</script>
</body>
</html>
网友评论