screen_rotation
Copied to Clipboard
<html> <head> <meta charset="UTF-8"> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> </head> <body> <div id="app"> <h3>Vue Js Get Class of Clicked Element</h3> <div @click="getClass($event)" class='class1'>Click me</div> <button @click="getClass($event)" class="class2">Button 1</button> <button @click="getClass($event)" class="class3">Button 2</button> <button @click="getClass($event)" class="class4">Button 3</button> </div> <script type="module"> const app = new Vue({ el: "#app", methods: { getClass(event) { const clickedElement = event.target; const classes = clickedElement.classList; alert(classes); // logs the classes of the clicked element } } }); </script> <style scoped> #app { display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100vh; } .class1, .class2, .class3, .class4 { background-color: #2196f3; color: #fff; padding: 12px 24px; font-size: 18px; font-weight: bold; border: none; border-radius: 4px; cursor: pointer; transition: background-color 0.2s ease-in-out; } .class1:hover, .class2:hover, .class3:hover, .class4:hover { background-color: #0d8bf2; } </style> </body> </html>