【发布时间】:2020-05-25 13:21:45
【问题描述】:
当我关注标签下方的输入元素时,我需要向标签元素添加类。我目前的解决方案是这样的:
HTML:
<label for="formProductId" ref="productIdLabel" class="form-element-title">Product ID</label>
<input id="formProductId" @blur="toggleFocus('productIdLabel', false)" @focus="toggleFocus('productIdLabel', true)" v-model="filterValues.productId" :name="filterOptions.productId === true ? 'productId' : false" type="text">
JS:
toggleFocus(ref: string, enable: boolean) {
if (enable) {
(this.$refs[ref] as HTMLElement).classList.add("js-focused");
} else {
(this.$refs[ref] as HTMLElement).classList.remove("js-focused");
}
}
我想删除 ref 属性并完全由所选元素本身切换以 js 为中心的类。如何选择最近的标签元素并编辑它的类?
【问题讨论】:
标签: javascript vue.js vuejs2