欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue.js做select下拉列表的實例(ul-li標簽仿select標簽)

 更新時間:2018年03月02日 15:03:54   作者:小m077  
下面小編就為大家分享一篇Vue.js做select下拉列表的實例(ul-li標簽仿select標簽),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

目標:用ul-li標簽結(jié)合Vue.js知識做一個模仿select標簽的下拉選項列表。

知識點:

組件的寫法及運用

組件之間的數(shù)據(jù)傳遞(props的運用)

組件之間的數(shù)據(jù)傳遞($emit的運用)

動態(tài)數(shù)據(jù)的綁定(v-bind)

自定義事件通信

效果圖:

1、未做任何操作前,下拉列表為隱藏狀態(tài)

2、點擊輸入框顯示下拉列表

3、 點擊列表項,輸入框值跟隨改變

PS: 為了演示data1, data2兩組數(shù)據(jù)的綁定,實例中創(chuàng)建了兩個列表

html代碼:

<!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>
 <div 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>
 </div>
</body>
</html>

JavaScript代碼

<script type="text/javascript">
//注冊全局組件
//在my-select組件中套用ul-select組件,my-select為父組件ul-select為子組件
Vue.component('my-select', {
 //組件中data要寫成函數(shù)形式
 data() {
  return {
   ulShow: false, //默認ul不顯示,單擊input改變ul的顯示狀態(tài)
   selectVal: '' //選項值,input的值與選項值動態(tài)綁定
  }
 },
 //父組件向子組件通信用props
 props: ['btnName', 'list'],
 template: `
  <div id="selectWrap">
   <div class="searchBox">
    <input type="text" :value="selectVal" @click='ulShow = !ulShow'/>
    <a href="#" rel="external nofollow" class="search" v-text='btnName'></a>
   </div>
    <my-ul v-show='ulShow' v-bind:list='list' v-on:receive='changeVal'></my-ul>
   </div>
 `,
 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) {
   //$emit觸發(fā)當前實例上的自定義事件 receive
   this.$emit('receive', item);
  }
 }
})
//創(chuàng)建Vue實例
new Vue({
 el: '#demo',
 //定義兩組數(shù)據(jù)分別傳遞到兩個組件的li中,兩個列表的操作互不影響
 data: {
  data1: ['CSS', 'HTML', 'JavaScript'],
  data2: ['Vue.js', 'Node.js', 'Sass'],
 }
})
</script>

CSS樣式

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;
}

以上這篇Vue.js做select下拉列表的實例(ul-li標簽仿select標簽)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論