admin 评论(0) 2021-06-29 HTML/CSS

在vue里面,是不提倡进行dom操作的,如果非进行dom的话,vue2.0里面有一个ref的属性,是可以达到dom的效果的。那么接下来我们不接住dom来进行操作:


由于习惯了webpack和vue-cli脚手架,所以楼主所有vue的代码都是放在webpack的脚手架当中进行,还使用了pug和scss的预处理器,vue的代码如下:


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

  ul

    li(v-for="(item,index) in classArr", @click="result(index)", :class="resultNum === index?'active':''") this is {{item}}

  li {

    list-style: none;

    width: 100px;

    margin-top: 10px;

    border: 1px solid red;

    &:hover {

      cursor: pointer;

    }

  }

  .active{

    background-color: aqua;

  }

  export default{

    data(){

      return {

        classArr: ["one", "two", "three"],

        num:"",

      }

    },

    methods: {

        result(index){

          this.num = index;

        }

    },

    computed:{

        resultNum(){

          return this.num;

      }

    }

  }

  思路如下:


这段代码使用的是index这个关键字,还使用了computed这个计算属性,当当前的index索引与点击的当前元素的下标相同的时候,便会触发active这个类名。说的很简练,不懂的可以加博主一起探讨


https://www.cnblogs.com/mmykdbc/p/8108205.html

评论
    你来打破0评论