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