Vuejs實現列表選中效果

//html
<div id="app">
  <ul><div>選擇你最喜歡的愛好:</div>
      <li class="collection-item"
         v-for="gameName in gameNames"
         :class="{active: activeName == gameName}"
         @click="selected(gameName)">{{gameName}}
        </li> 
</ul>
</div>
//js
new Vue({
  el: "#app",
  data: {
    gameNames: ['看書', '聽歌', '游泳', '健身', '看電影','旅遊'],
    activeName: ''
  },
  methods: {
    selected: function(gameName) {
      this.activeName = gameName
    }
  }
})
//css樣式
.active{
  color:red;
}
.collection-item{
  border:1px solid #dadada;
  padding:10px 2px;
  width:98px;
  margin-bottom:5px;
  text-align:center;
}

在線查看效果
https://jsfiddle.net/fozero/npxo3ryq/1/css

相關文章
相關標籤/搜索