JIYIK CN >

Current Location:Home > Learning > WEB FRONT-END > Vue >

How to find all elements by class name in Vue

Author:JIYIK Last Updated:2025/03/01 Views:

Vue is a very powerful JavaScript framework that provides developers with a lot of convenient features and tools. One of them is finding all elements by class name. In this article, we will explore how to find all elements by class name in Vue and provide some practical examples.

First, let's look at how to find a single element by class name in Vue. In Vue, you can use the $refs property to find a single element. For example, suppose we have a div element with the class name "my-class":

<div class="my-class" ref="myDiv">这是一个 div 元素</div>

To find this element by class name, we can use the following code:

this.$refs.myDiv

This will return a reference to the element. Now, let's see how to find all elements by class name.

In Vue, we can use the $el property to access the root element of a component. You can then use the querySelectorAll() method to find all elements by class name. For example, suppose we have a component that contains multiple div elements with the class name "my-class":

<template>
  <div>
    <div class="my-class">This is the first div element</div>
    <div class="my-class">This is the second div element</div>
    <div class="my-class">This is the third div element</div>
  </div>
</template>

To find all of these elements by class name, we can use the following code:

this.$el.querySelectorAll('.my-class')

This will return a NodeList object containing all elements with the class name "my-class". You can use a for loop to iterate through these elements and perform any desired operations. For example, the following code will iterate through all elements with the class name "my-class" and set their content to "Hello World!":

let elements = this.$el.querySelectorAll('.my-class')
for (let i = 0; i < elements.length; i++) {
  elements[i].textContent = 'Hello World!'
}

Now, let's look at some real-world examples.

Example 1 : Set the background color of all elements with the class name "my-class" to red

<template>
  <div>
    <div class="my-class">This is the first div element</div>
    <div class="my-class">This is the second div element</div>
    <div class="my-class">This is the third div element</div>
  </div>
</template>

<script>
export default {
  mounted() {
    let elements = this.$el.querySelectorAll('.my-class')
    for (let i = 0; i < elements.length; i++) {
      elements[i].style.backgroundColor = 'red'
    }
  }
}
</script>

Example 2 : Set the value of all input elements with the class name "my-class" to "Hello World!"

<template>
  <div>
    <input type="text" class="my-class">
    <input type="text" class="my-class">
    <input type="text" class="my-class">
  </div>
</template>

<script>
export default {
  mounted() {
    let elements = this.$el.querySelectorAll('.my-class')
    for (let i = 0; i < elements.length; i++) {
      elements[i].value = 'Hello World!'
    }
  }
}
</script>

In these examples, we used the $el property and querySelectorAll()method to find all elements by class name and performed some operations. These examples can help you better understand how to find all elements by class name in Vue and provide more practical functions for your project.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Configuring Apache Web Server on Ubuntu and Debian

Publish Date:2025/04/05 Views:176 Category:OPERATING SYSTEM

This article shows you how to install Apache web server on Ubuntu and Debian, set it up, and access the access logs. Apache Web Server in Ubuntu and Debian Apache HTTP Server is a free and open source web server that is very popular. More t

How to use Docker to image a Node.js web application

Publish Date:2025/03/26 Views:107 Category:Docker

Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at

My understanding of webservice is this

Publish Date:2025/03/18 Views:150 Category:NETWORK

Recently, I encountered such a project at work (temporarily named Project A). Project A itself was developed in PHP, but its data came from another project developed in Java (temporarily named Project B). Project A could not operate the dat

Which technology do you choose to implement the web chat room?

Publish Date:2025/03/18 Views:62 Category:NETWORK

With the rise of HTML5 Websockets, web chat applications are becoming more and more popular. Recently, I am working on a mobile web application, the core function of which is to implement web chat on the mobile phone. Of course, the functio

How to redirect a website from HTTP to HTTPS

Publish Date:2025/03/16 Views:117 Category:NETWORK

HTTPS is a protocol for secure communication over computer networks and is widely used on the Internet. More and more website owners are migrating from HTTP to HTTPS, mainly due to the following 5 reasons: Google announced that websites usi

How to avoid soft 404 in the website during SEO

Publish Date:2025/03/17 Views:136 Category:NETWORK

This article shares an SEO problem, soft 404. A status code we often see on websites is 404. Whether we develop a website or not, this is a problem we have to face. What is a soft 404? Before talking about soft 404, we must first understand

Find all elements by class name in React

Publish Date:2025/03/03 Views:157 Category:React

Find all elements by className in React: Use the getElementsByClassName method to get all elements with a specific class. Place the call to this method in the useEffect() hook. import {useEffect} from react ; const App = () = { useEffect }

Webpack packages ES6 and CommonJs mixed React

Publish Date:2025/03/02 Views:179 Category:React

This article mainly introduces how to use webpack to package and compile React mixed with ES6 and CommonJs. It is a process of upgrading the React environment.

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial