<!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>
vue 1.0 模仿百度搜索下拉框
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。