本文摘自PHP中文网,作者php中世界最好的语言,侵删。
这次给大家带来Vue.jS的ul-li标签仿select标签,使用Vue.jS的ul-li标签仿select标签注意事项有哪些,下面就是实战案例,一起来看一下。目标:用ul-li标签结合Vue.js知识做一个模仿select标签的下拉选项列表。
知识点:
组件的写法及运用
组件之间的数据传递(props的运用)
组件之间的数据传递($emit的运用)
动态数据的绑定(v-bind)
自定义事件通信
效果图:
1、未做任何操作前,下拉列表为隐藏状态

2、点击输入框显示下拉列表

3、 点击列表项,输入框值跟随改变

PS: 为了演示data1, data2两组数据的绑定,实例中创建了两个列表

html代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <!DOCTYPE html>
<html>
<head>
<meta charset= "UTF-8" >
<title>ul-li模仿select下拉菜单</title>
<link rel= "stylesheet" type= "text/css" href= "style.css" rel= "external nofollow" />
<script src= "https://unpkg.com/vue/dist/vue.js" ></script>
</head>
<body>
<p id= "demo" >
<my-select btn-name= 'search' v-bind:list= 'data1' style= 'float: left;margin-right: 2rem;' ></my-select>
<my-select btn-name= '搜索' v-bind:list= 'data2' style= 'float: left;' ></my-select>
</p>
</body>
</html>
|
JavaScript代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | <script type= "text/javascript" >
Vue.component( 'my-select' , {
data() {
return {
ulShow: false,
selectVal: ''
}
},
props: [ 'btnName' , 'list' ],
template: `
<p id= "selectWrap" >
<p class = "searchBox" >
<input type= "text" :value= "selectVal" @click= 'ulShow = !ulShow' />
<a href= "#" rel= "external nofollow" class = "search" v-text= 'btnName' ></a>
</p>
<my-ul v-show= 'ulShow' v-bind:list= 'list' v-on:receive= 'changeVal' ></my-ul>
</p>
`,
methods: {
changeVal(value) {
this.selectVal = value
}
}
})
Vue.component( 'my-ul' , {
props: [ 'list' ],
template: `
<ul class = "skill" >
<li v- for = 'item of list' v-on:click= 'selectLi(item)' >{{item}}</li>
</ul>
`,
methods: {
selectLi: function (item) {
this. $emit ( 'receive' , item);
}
}
})
new Vue({
el: '#demo' ,
data: {
data1: [ 'CSS' , 'HTML' , 'JavaScript' ],
data2: [ 'Vue.js' , 'Node.js' , 'Sass' ],
}
})
</script>
|
CSS样式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | ul, li {
margin: 0;
padding: 0;
list-style: none;
}
#selectWrap {
width: 250px;
padding: 2rem;
background: #4682b4;
}
.searchBox input, .searchBox a {
line-height: 1.5rem;
height: 1.5rem;
margin-bottom: 1rem;
padding: 0 5px;
vertical-align: middle;
border: 1px solid #aaa;
border-radius: 5px;
outline: none;
}
.searchBox a {
display: inline-block;
text-decoration: none;
background-color: #b1d85c;
}
.skill li {
font-size: 18px;
line-height: 2rem;
height: 2rem;
padding-left: 5px;
cursor: pointer;
}
.skill li:hover {
background-color: #008b45;
}
|
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
使用javascript的模块加载器
v-for怎么加载本地静态图片
以上就是Vue.jS的ul-li标签仿select标签的详细内容,更多文章请关注木庄网络博客!
相关阅读 >>
html5中的script属性及js同步和异步加载实现代码详解
javascript怎么替换空格
javascript和typescript的区别是什么
多行文本进行截断的奇淫巧技
移动端h5中百度地图的click事件的介绍(代码示例)
理解javascript中的closure(闭包)
javascript中怎么换行
h5的视频播放库video.js详解
javascript字符串比较方法有哪些
javascript怎么移除属性
更多相关阅读请进入《javascript》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » Vue.jS的ul-li标签仿select标签