<!DOCTYPE html>
<html>
<head>
<title>模仿百度API</title>
<meta charset="utf-8">
<script type="text/javascript" src="js/vue-1.js"></script>
<script type="text/javascript" src="js/vue-resource.js"></script>
<style type="text/css">
.gray{
background: #ccc;
}
</style>
</head>
<body>
<div id="box">
<input type="text" v-model="keyWord" @keyup="get($event)" @keydown.down="changDown()" @keydown.up.prevent="changUp()" />
<ul>
<li v-for="v in lists" :class="{gray:$index==now}">
{{v}}
</li>
</ul>
<p v-show="lists.length==0">暂无数据....</p>
</div>
</body>
</html>
<script type="text/javascript">
new Vue({
el:"#box",
data:{
keyWord:'',
lists:[],
now:-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.keyWord)
this.keydown='';
}
this.$http.jsonp("https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su",{
wd:this.keyWord
},{
jsonp:'cb'
}).then(function(res){
this.lists = res.data.s;
},function(res){
console.log(res.data);
})
},
changDown:function(){
this.now++;
if (this.now == this.lists.length) {
this.now = -1;
};
this.keyWord=this.lists[this.now];
},
changUp:function(){
this.now--;
if (this.now == -2) {
this.now = this.lists.length-1;
};
this.keyWord=this.lists[this.now];
}
}
})
</script>
const express = require("express");
const expressStatic = require("express-static");
const bodyParser = require('body-parser');
var server = express();
server.use(bodyParser.urlencoded());
server.listen(8080);
server.use(expressStatic("./www"));