1、在要获取的标签中添加 ref="xx"

示例:

<button ref="btn">一个按钮</button>

 

2、在 mounted 钩子中使用 this.$refs.xx. 获取并操作 DOM 元素

示例:

mounted() {
    this.$refs.btn.style.backgroundColor="red"
}

 

3、vue 操作 DOM 完整示例:

template 部分:

<template>
  <div>
    <button ref="btn">一个按钮</button>
  </div>
</template>

 script 部分:

<script>
export default {
  data () {
    return {

    }
  },
  mounted () {
    this.$refs.btn.style.backgroundColor = 'red'
  }
}
</script>

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-07
  • 2022-12-23
  • 2021-11-07
  • 2021-11-25
  • 2022-01-08
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-07-07
  • 2022-12-23
  • 2021-08-12
  • 2022-12-23
相关资源
相似解决方案