简体   繁体   English

Vue.js - 在Click事件上添加类

[英]Vue.js - Add Class on Click Event

I would like to dynamically toggle a class on a click event on a div in Vue.js without using properties/data to do so. 我想动态切换Vue.js中div上的click事件的类,而不使用属性/数据来执行此操作。

Here is my div 这是我的div

<div class="quiz-item" @click="checkAnswer(1)">

When this div is clicked, I would like to add the class quiz-item--correct or quiz-item--incorrect (the logic for this will be handled elsewhere). 单击此div ,我想添加类quiz-item--correctquiz-item--incorrect (此逻辑将在其他地方处理)。 I cannot use properties as there are too many answers in the quiz for it to be a maintainable/viable approach. 我无法使用属性,因为测验中有太多答案,因为它是一种可维护/可行的方法。

Does anyone have any ideas on how I can achieve this functionality? 有没有人对如何实现这一功能有任何想法?

You can do something like this: 你可以这样做:

<div class="quiz-item" @click="$event.target.classList.toggle('classname')">

You can check the fiddle demonstrating this: Here 你可以检查一下小提琴演示: 这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM