1👍
If you’re going to use Vue, use Vue. Think of the DOM as a side effect of the app state, don’t go rummaging around adding and removing classes on your own.
Instead of DOM traversals, use Vue’s $refs to identify specific elements (when necessary. It often isn’t.)
// in the template:
<a ref="foo">...</a>
// in the component methods:
this.$refs.foo // <-- the anchor tag. "this" is the component itself.
Instead of adding or removing classes manually, set state variables. The template can check those variables when deciding which classes to include; no DOM traversal necessary.
<div :class="{'large-3': isFoo, 'large-6': !isFoo}">...</div>
or
<div v-if="isFoo" class="large-3">...</div>
<div v-if="!isFoo" class="large-6">...</div>
data: {
isFoo: true
},
methods: {
foo() { this.isFoo = !this.isFoo } // toggle this boolean to swap the classnames
}
1👍
You want to use event.currentTarget
instead of event.target
.
[event.currentTarget] Identifies the current target for the event, as the event traverses the DOM. It always refers to the element to which the event handler has been attached, as opposed to event.target which identifies the element on which the event occurred.