亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 VueJS 中切換按鈕的背景顏色

如何在 VueJS 中切換按鈕的背景顏色

料青山看我應如是 2023-10-23 10:49:57
我正在使用 VueJS,并且有一個動態創建的流派按鈕列表。這些按鈕能夠打開和關閉,當它們打開時,我想應用 CSS 樣式(如藍色背景)來顯示它們已被選中。當它們沒有被選擇時,我希望它回到原來的樣子(帶有灰色邊框的白色背景)。我的問題是我無法實現切換背景顏色功能。我嘗試過使用variant="primary"以及其他變體選項,但它不起作用。我也嘗試過style="border: 2px solid darkgrey; margin:2px;"在應用時擦除variant,但這也不起作用。我嘗試使用StackOverflow post直接更改按鈕的 CSS ,但它不起作用。這是我的 index.html 中的按鈕標記:<ul style="list-style: none;">    <button v-if="ShowALLButton" v-on:click="getAllGenres" class = "btn btn-success" style="left: 0px; margin:2px;">ALL</button>        <li style="display: inline;">          <button v-for="buttons in buttonsList" v-if="ShowALLButton" v-on:click="getGenre(buttons)" :pressed.sync="buttons.state" id="buttonID" class = "btn" style="border: 2px solid darkgrey; margin:2px;">            {{ buttons.genre }}.           </button>        </li></ul>本質上,上面是一個 for 循環迭代我的 ButtonList,它是一個看起來像這樣的結構,包含要在按鈕上顯示的類型,以及它是否已打開或關閉(由狀態指示,即最初關閉,如false) 所示。實際的切換功能有效,因為它顯示了正確的流派結果。但對于那些好奇的人來說,buttonList 看起來像這樣(對象列表):[{ genre: "Folk", state: false },{ genre: "Rap", state: false },{ genre: "Pop", state: false },...]index.html 中的相關樣式:<style> .am-active {    background-color: rgb(21, 79, 202);    color: white; } .am-not-active {    background-color: white; }</style>處理按鈕切換和流派選擇的函數位于我的 script.js 中。我需要todo!應用正確的 CSS 樣式來更改按鈕,但我無法做到。// Get Selected GenresgetGenre(button) {  console.log("ENTERED GET GENRE!!!!!!");  var selectedGenresItems = []  // keep track of selected genres  if (!this.selectedGenres.includes(button)) {    this.selectedGenres.push(button);    // todo!    console.log("inside if");    button.className += 'am-active';    console.log(button);    // ^ the above does not work, it just adds a key with "className" = "undefinedam-active" :(  }  else {    this.selectedGenres.pop(button);    // todo! must use .am-not-active  }
查看完整描述

1 回答

?
SMILET

TA貢獻1796條經驗 獲得超4個贊

您可以根據狀態的鍵綁定屬性class并啟用/禁用一個類。statebuttonsList


new Vue({

  el: '#app',

  

  data: {

    buttonsList: [

      { genre: "Folk", state: false },

      { genre: "Rap", state: false },

      { genre: "Pop", state: false },

    ]

  },

  

  methods: {

    getGenre (buttons, i) {

      this.buttonsList[i].state = !this.buttonsList[i].state

    }

  }

})

.am-active {

    background-color: rgb(21, 79, 202);

    color: white;

 }

 .am-not-active {

    background-color: white;

 }

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>


<div id="app">


  <ul>

    <li>

      <button 

        v-for="(buttons, i) in buttonsList" v-on:click="getGenre(buttons, i)"

        :class="{

          'am-active': buttons.state,

          'am-not-active': !buttons.state

        }"

        :key="i"

      >

        {{ buttons.genre }}. 

      </button>

    </li>

  </ul>


</div>


查看完整回答
反對 回復 2023-10-23
  • 1 回答
  • 0 關注
  • 201 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號