鍍金池/ 問答/數(shù)據(jù)分析&挖掘  HTML/ 在Vue.js中,點擊按鈕的時候當前的按鈕變色,因為按鈕是循環(huán)出來的,我現(xiàn)在一點

在Vue.js中,點擊按鈕的時候當前的按鈕變色,因為按鈕是循環(huán)出來的,我現(xiàn)在一點擊,下面所有的按鈕都變色了

在Vue.js中,點擊按鈕的時候當前的按鈕變色,因為按鈕是循環(huán)出來的,我現(xiàn)在一點擊,下面所有的按鈕都變色了

html如下
        <div id="app">
        <div v-for="(item,key) in data" class="home">
            <span>{{item.title}}</span>
            <button  class="btn anni1" @click="one">點擊1</button>
            <button class="btn anni1" @click="two">點擊2</button>
            <button class="btn anni1" @click="three">點擊3</button>
        </div>    
    </div>

js如下

    <script type="text/javascript">
        new Vue({
            el:"#app",
            data :{
                data:[
                    {title:"111"},
                    {title:"2222"},
                    {title:"333"}
                ]
            },
            methods: {
                one(){
                    
                },
                two(){
                    
                },
                three(){
                    
                }
            }
        })
    </script>


頁面的效果圖是這樣的

clipboard.png

我想要的是我點擊的時候

clipboard.png

我現(xiàn)在總是成為

clipboard.png

但是我想要每個獨立起來 怎么實現(xiàn)呢

回答
編輯回答
離人歸

圖片描述

圖片描述

2017年1月23日 08:38
編輯回答
墻頭草

YYF? 兄弟玩dota2嗎?
循環(huán)和按鈕可自定義變量控制
代碼如下
HTML

html如下
        <div id="app">
        <div v-for="(item,key) in data" :key="key" class="home">
            <span>{{item.title}}</span>
            <button  class="btn anni1" :class="{'color' : oneThree === key && hIndex === 0 }" @click="one(key)">點擊1</button>
            <button class="btn anni1" :class="{'color' : twoThree === key && hIndex === 1 }" @click="two(key)">點擊2</button>
            <button class="btn anni1" :class="{'color' : twoThree === key && hIndex === 2 }"  @click="three(key)">點擊3</button>
        </div>    
    </div>

JS

    <script type="text/javascript">
        new Vue({
            el:"#app",
            data :{
                data:[
                    {title:"111"},
                    {title:"2222"},
                    {title:"333"}
                ],
                oneIndex:null,
                twoIndex:null,
                threeIndex:null,
                hIndex:null
            },
            methods: {
                one(key) {
                  this.oneIndex = key;
                  this.isKey = 0;
                },
                two(key) {
                  this.twoIndex = key;
                  this.isKey = 1;
                },
                three(key) {
                  this.threeIndex = key;
                  this.isKey = 2;
                }
            }
        })
    </script>


還有 可以 直接給data里加上個屬性isClicked:false.點擊變化當前行的isClicked就可以了.
希望有幫助

2017年7月22日 11:21
編輯回答
孤影

你這不能叫循環(huán)出來的按鈕了。循環(huán)都沒在按鈕上。希望以后問題的代碼能貼全。

<div v-for="(item, index) in data" class="home" :key="index">
  <span>{{item.title}}</span>
  <button  class="btn anni1" :class="{'red': item.activeIndex === 1}" @click="setActive(item, 1)">點擊1</button>
  <button class="btn anni1" :class="{'red': item.activeIndex === 2}" @click="setActive(item, 2)">點擊2</button>
  <button class="btn anni1" :class="{'red': item.activeIndex === 3}" @click="setActive(item, 3)">點擊3</button>
</div>
setActive (item, num) {
  this.$set(item, 'activeIndex', num) //item里沒有activeIndex,動態(tài)添加屬性要用$set
}
2018年6月20日 03:06
編輯回答
不舍棄
<div id="app">
        <div v-for="(item,key) in data" class="home">
            <span>{{item.title}}</span>
        </div>    
        <button  class="btn anni1" @click="one">點擊1</button>
        <button class="btn anni1" @click="two">點擊2</button>
        <button class="btn anni1" @click="three">點擊3</button>
    </div>
2017年8月22日 02:47