Vue获取文本值,获取点击循环列表的值
可能单一获取文本值容易,但是网上的查到的方法都是event.target.innerText这个。这个是只能获取那种不循环的值
<h2 v-on:click="select()">文本内容</h2>
在methods中获取文本值
methods: {
select: function (event) {
console.log(‘您选中的内容是》》 ’ + event.target.innerText);//打印得到了Ecosystem
}
}
效果图
在当遇到循环的列表获取文本值的时候,上面的方法是行不通的,得用新的方法
//循环列表内容
<div v-for="(item, index) in typeList" :key="index" class="testTypeBtn" tabindex="1" @click="selectItem(index)">{{ item.name }}</div>
data( ){
typeList:[ ] //循环定义的数组
},
methods:{
selectItem(e){
//用this.typeList[e].name可以,name为循环中的要获取的文本值的字段
console.log('您选中的内容是》》》' + this.typeList[e].name)
},
}