Home > Web Front-end > H5 Tutorial > How to use index to add class to the first item in Vuejs

How to use index to add class to the first item in Vuejs

php中世界最好的语言
Release: 2018-03-27 16:35:18
Original
2265 people have browsed it

This time I will bring you how to use index to add class to the first item in Vuejs. Notes on using index to add class to the first item in Vuejsare Which ones, the following are practical cases, let’s take a look.

(1) In v-for, use index to add class to the first item

View the code piece on CODE derived to My code piece

<a class="list-group-item" :class="{&#39;active&#39;:!index}" v-for="(i,index) in announcement">
Copy after login

index comes from v-for, i represents the element of array traversed, and index represents index.

Since index starts from 0, if you want to specify that the first item has the active class, then use v-bind:class="{'active':!index}"

One item was originally false (0), and the second item and the subsequent item were true (>0). Their values ​​are reversed through the logical NOT operator .

Therefore, the first item has the active class, but the following items do not.

If there is a class other than the first item, it can also be achieved by not adding the logical NOT operator.

Similarly, you can use an expression such as index==2 to let the third item obtain this class.

Note, My version is Vuejs2.0, so it seems that it cannot be replaced with $index.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Web-side application implements back force refresh

Detailed explanation of JS image processing and synthesis

The above is the detailed content of How to use index to add class to the first item in Vuejs. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template