javascript - How to remove particular class name from an selected element in VueJs? - Stack Overflow

This is what my vuejs methods looks like. Here in the changeRoute function I can change class name by e

This is what my vuejs methods looks like. Here in the changeRoute function I can change class name by e.target.className = 'clicked'; But when I try to remove that class name from other elements I cant do it by pre.removeClass('clicked'); How can I acplish this ?

<script>   
export default {

  ponents: { 
      
  },
  data() { 
      return {

      }
  },
  methods: {
      changeRoute(e, route) {
           
        var pre = this.$el.querySelector('.clicked');
        if(pre) {
            // pre.removeClass('clicked'); 
        }

        this.$router.push({
            name: route
        }); 
        e.target.className = 'clicked';
          
      }
  },
  mounted() {
      this.$nextTick(() => {
 
      })
  }
}

</script>

This is what my vuejs methods looks like. Here in the changeRoute function I can change class name by e.target.className = 'clicked'; But when I try to remove that class name from other elements I cant do it by pre.removeClass('clicked'); How can I acplish this ?

<script>   
export default {

  ponents: { 
      
  },
  data() { 
      return {

      }
  },
  methods: {
      changeRoute(e, route) {
           
        var pre = this.$el.querySelector('.clicked');
        if(pre) {
            // pre.removeClass('clicked'); 
        }

        this.$router.push({
            name: route
        }); 
        e.target.className = 'clicked';
          
      }
  },
  mounted() {
      this.$nextTick(() => {
 
      })
  }
}

</script>

Also how can push class name rather than replace all by e.target.className = 'clicked';

Share Improve this question asked May 30, 2018 at 8:27 SwadhInSwadhIn 7471 gold badge10 silver badges19 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

You can use classList.add and classList.remove for this.
But it seems like you want to style link depending on the current route which can be done with vue-router as it adds class on the links matching the current route.
Vue-router active class

If the selected element is part of a li list and class should be added (and consequently removed from previous clicked li), here's a good solution by Herteby on the VueJS Forum

https://forum.vuejs/t/how-to-add-active-class-to-element/28108/2

Use actual Vue to acplish this task. I could explain here how but the Vue page does it quite well.

https://v2.vuejs/v2/guide/class-and-style.html

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742313842a4420436.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信