vue中获取v-for中的dom元素

时间:2024-03-09 09:17:29

在ready下时,v-for循环出的dom元素还添加进dom树中,所以也不能获取到,如果在vue中获取相应的dom的话,通过$nextTick可以获取到,在修改数据后立即使用它,他会延时回调该方法,this是绑定在调用它的实例上 比如:
methods: {
// ...
example: function () {
// 修改数据
this.message = \'changed\'
// DOM 还没有更新
this.$nextTick(function () {
// DOM 现在更新了
// this 绑定到当前实例
this.doSomethingElse()
})
}
}