How does vue2 get the elements corresponding to the subscript index
<ul class="left_ul_1">
<li class="left_li_1" v-for="(item,index) in plotList" :key="item.id" @click="plotClick(item.id,index)">{{item.name}}
<i class="aui-iconfont aui-icon-right"></i>
</li>
</ul>
plotClick: function(id,index) {
alert(index)
when I click on each li, I have got the subscript of each li. How to get the li, corresponding to the subscript for other operations. Except for $event
if you want to change classname, you don't need to bind dom: class. Just get a corresponding attribute to control the addition and removal of li's class.
for example
<ul class="left_ul_1">
<li class="left_li_1" :class="{'active': current === index }" ref="li" v-for="(item,index) in plotList" :key='item.id' @click="plotClick(item.id,index)">{{item.name}}
<i class="aui-iconfont aui-icon-right"></i>
</li>
</ul>
// data
data(){
return{
current: null
}
},
///====
plotClick: function(id,index) {
this.current = index;
}
There is no need to get dom elements in
vue, just change the data
<ul class="left_ul_1">
<li class="left_li_1" ref="li" v-for="(item,index) in plotList" :key='item.id' @click="plotClick(item.id,index)">{{item.name}}
<i class="aui-iconfont aui-icon-right"></i>
</li>
</ul>
plotClick: function(id,index) {
console.log(this.$refs.li[index])
there is no need to get li, and then manipulate dom, to manipulate data directly. If you want to manipulate css,class, just write the corresponding class in your data. If useclass true, use class a, or b.
<li class="left_li_1" :class="[item.useclass? 'a':'b']" v-for="(item,index) in plotList" :key='item.id' @click="plotClick(item.id,index)">{{item.name}}
<i class="aui-iconfont aui-icon-right"></i>
</li>