<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="vue.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue-resource/1.3.4/vue-resource.min.js"></script>
<style>
.gray {
background-color: #ccc;
}
</style>
<script>
window.onload = function () {
new Vue({
el: '#box',
data: {
myData: [], //搜索下拉数据列表
t1: '', //输入框里的值
now: -1 //输入框里面的值得索引,0位列表第一项
},
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.$http.jsonp('https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su', {
params: {
wd: this.t1 // 输入的关键词
},
jsonp: 'cb' //callback函数的名称
}).then(function (res) {
this.myData=res.data.s;
console.log(this.myData)
}, function (err) {
console.log(err.status);
});
},
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];
},
dataLink: function (index) { //鼠标点击跳转
this.t1 = this.myData[index];
this.now = index;
window.open('https://www.baidu.com/s?wd=' + this.t1);
this.t1 = ''
}
}
})
}
</script>
</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,index) in myData" :class="{gray:index==now}">
{{ value }}
</li>
</ul>
<p v-show="myData.length==0">暂无数据...</p>
</div>
</body>
</html>