vue实现选中li变色--小技巧


先上代码

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <style>
    .on{
      color: red;
    }
  </style>
</head>
<body>
  <div id="app">
    <ul>
      <li :class="{'on':indexList.indexOf(index)>-1}" v-for="(item,index) in heros" @click="change(index)">{{item}}</li>
    </ul>
  </div>
</body>
<script src="../../js/vue.js"></script>
<script>

  new Vue({
    el:"#app",
    data:{
      heros:['雷恩加尔','安妮','沃里克','德莱厄斯'],
      indexList:[]
    },
    methods:{
      change(index){
        let arrIndex = this.indexList.indexOf(index);
        console.log(arrIndex)
        if (arrIndex > -1){
          this.indexList.splice(arrIndex,1)
        }else{
          this.indexList.push(index)
        }
        console.log(this.indexList)
      }
    }
  })
</script>
</html>

 

预览:

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM