鍍金池/ 問答/HTML5  HTML/ vue判斷dom的class

vue判斷dom的class

vue點擊給dom添加class然后獲取含有class的dom
<div class="chose-ck" v-for="(item,index2) in colors" :key="index2" ref="chosebox">

                                    <p>{{item.name}}</p>
                                    <dt v-for="(item2,index) in item.childsCurGoods" :key="item2.id" :class="index==iac[index2]?'check':''" :id="item2.id" :data-chosename="item.name" :data-choseidname="item2.name" :data-chose="item.id" :data-id="item2.id" @click="chek(index2,index)" >
                                        {{item2.name}}
                                    </dt>
                                </div>js
    chek(index2, index) {
            this.iac[index2] = index
            this.iac = this.iac.concat([]);
            this.checkchose()
        },
        checkchose:function(){
            var chose=this
            var chosedom=chose.$refs.chosebox
            console.log(chosedom)
            for (var i=0;i<chosedom.length;i++) {
                var children=chosedom[i].children
                for (var j=0;j<children.length;j++) {    
                    if(children[j].className=="check")
                    {
                        console.log(children[j])
                    }
                }
            }
            
        }

點擊過后獲取到的dom打印
圖片描述

if(children[j].className=="check")
加了判斷為什么打印出來的dom是點擊之前的dom
圖片描述

回答
編輯回答
安淺陌

html綁定的事件一個參數(shù)加上事件對象$event,js方法第一個參數(shù)寫成event,通過event.target.className獲取點擊的那個的class

2017年9月17日 07:32
編輯回答
愚念

vue是reactive的,用了vue就避免操作dom,綁定數(shù)據(jù)到dom上,通過修改數(shù)據(jù)修改dom,如果直接操作dom就完全拋棄了最大的優(yōu)勢。

2017年2月8日 04:38
編輯回答
法克魷

在綁定事件的地方 這樣寫 @click="fun(this)"
js `fun(e){
consoloe.log(e.target.className)//這e就代表你點擊當前按鈕
}`

2017年6月12日 21:32