admin管理员组文章数量:1332890
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';
3 Answers
Reset to default 2You 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
本文标签: javascriptHow to remove particular class name from an selected element in VueJsStack Overflow
版权声明:本文标题:javascript - How to remove particular class name from an selected element in VueJs? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742313842a2451414.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论