欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

VUE如何實(shí)現(xiàn)點(diǎn)擊文字添加顏色(動(dòng)態(tài)修改class)

 更新時(shí)間:2023年11月16日 09:38:36   作者:前端李小白  
這篇文章主要介紹了VUE如何實(shí)現(xiàn)點(diǎn)擊文字添加顏色(動(dòng)態(tài)修改class),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

vue點(diǎn)擊文字添加顏色(動(dòng)態(tài)修改class)

實(shí)現(xiàn)的原理很簡(jiǎn)單,只需要?jiǎng)討B(tài)綁定一個(gè)class就可以了,現(xiàn)在分別列出三種情況

1.點(diǎn)擊文字顏色改變

再次點(diǎn)擊,點(diǎn)擊的顏色改變,之前的顏色變回原來的顏色

代碼如下:

<template>
  <div class="list2">
    <ul>
      <li
        v-for="(item,index) in List"
        :key="index"
        :class="{activeColor:colorIndex===index}"
        @click="changeColor(item,index)"
      >
        <span>{{item.name}}</span>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  name: "list",
  components: {},
  data() {
    return {
      List: [
        { name: "總的" },
        { name: "部分1" },
        { name: "部分2" },
        { name: "部分3" }
      ],
      colorIndex: -1
    };
  },
  mounted() {},
  methods: {
    changeColor(item, index) {
      this.colorIndex = index;
    }
  }
};
</script>

<style lang="less" scoped>
.list2 {
  width: 100px;
  ul {
    list-style: none;
    .activeColor {
      background-color: #f6fbff;
      color: #298adb;
    }
    li {
      cursor: pointer;
    }
  }
}
</style>

效果如下,當(dāng)我點(diǎn)擊其中某一個(gè)文字的時(shí)候,字體的顏色就改變了

2.如果需要點(diǎn)擊多個(gè)變顏色的話

再次點(diǎn)擊取消的話,我是這樣做的,給data里面的對(duì)象添加一個(gè)屬性,全部設(shè)置為false,然后點(diǎn)擊的時(shí)候設(shè)置為true,

代碼如下:

<template>
  <div class="list2">
    <ul>
      <li
        v-for="(item,index) in List"
        :key="index"
        :class="{activeColor:item.active}"
        @click="changeColor(item,index)"
      >
        <span>{{item.name}}</span>
      </li>
    </ul>
  </div>
</template>
<script>
export default {
  name: "list",
  components: {},
  data() {
    return {
      List: [
        { name: "總的" },
        { name: "部分1" },
        { name: "部分2" },
        { name: "部分3" }
      ],
      colorIndex: -1
    };
  },
  mounted() {
      this.addActive()
  },
  methods: {
      /*給List的每一個(gè)對(duì)象添加active屬性,需要用到this.$set('對(duì)象','鍵值','value') */
      addActive(){
        this.List.forEach(item=>{
            this.$set(item,'active',false)
        })
      },
    changeColor(item, index) {
      if(!item.active){
         item.active = true
      } else {
          item.active = false
      }
    }
  }
};
</script>
<style lang="less" scoped>
.list2 {
  width: 100px;
  ul {
    list-style: none;
    .activeColor {
      background-color: #f6fbff;
      color: #298adb;
    }
    li {
      cursor: pointer;
    }
  }
}
</style>

效果如下:點(diǎn)擊多個(gè)文字可以改變樣式,再次點(diǎn)擊可以取消

3.有時(shí)候我們要是有個(gè)‘總的’

那個(gè)點(diǎn)擊‘總的’,就不能有部分的存在了,點(diǎn)擊部分就不能有‘總的’存在

代碼如下:

<template>
  <div class="list2">
    <ul>
      <li
        v-for="(item,index) in List"
        :key="index"
        :class="{activeColor:item.active}"
        @click="changeColor(item,index)"
      >
        <span>{{item.name}}</span>
      </li>
    </ul>
  </div>
</template>
<script>
export default {
  name: "list",
  components: {},
  data() {
    return {
      List: [
        { name: "總的" },
        { name: "部分1" },
        { name: "部分2" },
        { name: "部分3" }
      ],
      colorIndex: -1
    };
  },
  mounted() {
    this.addActive();
  },
  methods: {
    /*給List的每一個(gè)對(duì)象添加active屬性,需要用到this.$set('對(duì)象','鍵值','value') */
    addActive() {
      this.List.forEach(item => {
        this.$set(item, "active", false);
      });
    },
    changeColor(item, index) {
      if (index === 0) {
        this.List.forEach(item => {
          item.active = false;
        });
      } else {
        this.List[0].active = false;
      }
      if (!item.active) {
        item.active = true;
      } else {
        item.active = false;
      }
    }
  }
};
</script>
<style lang="less" scoped>
.list2 {
  width: 100px;
  ul {
    list-style: none;
    .activeColor {
      background-color: #f6fbff;
      color: #298adb;
    }
    li {
      cursor: pointer;
    }
  }
}
</style>

效果如下,點(diǎn)擊‘總的’話,部分會(huì)全部不變,點(diǎn)擊‘部分’,全部會(huì)不變

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue項(xiàng)目element-ui級(jí)聯(lián)選擇器el-cascader回顯的問題及解決

    vue項(xiàng)目element-ui級(jí)聯(lián)選擇器el-cascader回顯的問題及解決

    這篇文章主要介紹了vue項(xiàng)目element-ui級(jí)聯(lián)選擇器el-cascader回顯的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • 解決Vue數(shù)據(jù)更新了但頁(yè)面沒有更新的問題

    解決Vue數(shù)據(jù)更新了但頁(yè)面沒有更新的問題

    在vue項(xiàng)目中,有些我們會(huì)遇到修改完數(shù)據(jù),但是視圖卻沒有更新的情況,具體的場(chǎng)景不一樣,解決問題的方法也不一樣,在網(wǎng)上看了很多文章,在此總結(jié)匯總一下,需要的朋友可以參考下
    2023-08-08
  • vue.js實(shí)現(xiàn)刷新當(dāng)前頁(yè)面的方法教程

    vue.js實(shí)現(xiàn)刷新當(dāng)前頁(yè)面的方法教程

    這篇文章主要給大家介紹了關(guān)于vue.js實(shí)現(xiàn)刷新當(dāng)前頁(yè)面的方法教程,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。
    2017-07-07
  • vue操作select獲取option值方式

    vue操作select獲取option值方式

    文章介紹了在Vue中操作select元素并實(shí)時(shí)獲取選中的option值的方法,通過使用`@change`事件并傳遞參數(shù),可以實(shí)現(xiàn)動(dòng)態(tài)獲取選中的值
    2025-01-01
  • vue項(xiàng)目前端加前綴(包括頁(yè)面及靜態(tài)資源)的操作方法

    vue項(xiàng)目前端加前綴(包括頁(yè)面及靜態(tài)資源)的操作方法

    這篇文章主要介紹了vue項(xiàng)目前端加前綴(包括頁(yè)面及靜態(tài)資源)的操作方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2023-12-12
  • Vue.js集成Word實(shí)現(xiàn)在線編輯功能

    Vue.js集成Word實(shí)現(xiàn)在線編輯功能

    在現(xiàn)代Web應(yīng)用中,集成文檔編輯功能變得越來越常見,特別是在協(xié)作環(huán)境中,能夠直接在Web應(yīng)用內(nèi)編輯Word文檔可以極大地提高工作效率,本文將詳細(xì)介紹如何在Vue.js項(xiàng)目中集成Word在線編輯功能,需要的朋友可以參考下
    2024-08-08
  • Vue3中Hooks封裝的技巧詳解

    Vue3中Hooks封裝的技巧詳解

    這篇文章主要來和大家分享一些關(guān)于 Vue3中Hooks封裝的技巧,希望能夠?yàn)榇蠹以?nbsp;Vue 3 項(xiàng)目中更好地利用 Hooks 提供一些思路和實(shí)踐經(jīng)驗(yàn)
    2023-12-12
  • Vue3封裝實(shí)現(xiàn)右鍵菜單組件

    Vue3封裝實(shí)現(xiàn)右鍵菜單組件

    這篇文章主要為大家詳細(xì)介紹了Vue3封裝實(shí)現(xiàn)右鍵菜單組件的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以了解下
    2025-02-02
  • VueX學(xué)習(xí)之modules和namespacedVueX詳細(xì)教程

    VueX學(xué)習(xí)之modules和namespacedVueX詳細(xì)教程

    這篇文章主要為大家介紹了VueX學(xué)習(xí)之modules和namespacedVueX詳細(xì)教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • 基于Vuejs實(shí)現(xiàn)購(gòu)物車功能

    基于Vuejs實(shí)現(xiàn)購(gòu)物車功能

    這篇文章主要為大家詳細(xì)介紹了基于Vuejs實(shí)現(xiàn)購(gòu)物車功能的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-08-08

最新評(píng)論