Vue中的DOM操作


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>

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM