Vuejs getting the element that is being called by an event?

后端 未结 1 2104
猫巷女王i
猫巷女王i 2021-02-15 18:49

I have multiple list items that I want to toggle active classes on when they are being clicked on.

1条回答
  •  忘了有多久
    2021-02-15 19:06

    You can directly pass the event and the events target in your function. The target is the element you clicked on.

    HTML:

    • Trigger a handler
    • Trigger an expression

    JS:

    var vue = new Vue({
      el: '#demo',
      data: {},
      methods: {
        onClick: function (e) {
          var clickedElement = e.target;
        }
      }
    })
    

    With your element you can do what you want. For example, if you use jQuery:

    $(clickedElement).siblings().removeClass('active');
    $(clickedElement).addClass('active');
    

    0 讨论(0)
提交回复
热议问题