vue進頁面input自動獲取焦點


1.ref實現,要寫在mounted里面

<input type="text" v-model="name" ref="getFocus" />

<script>
export default {
  data() {
    return {
      name: ''
    }
  },
  mounted() {
    this.$refs.getFocus.focus()
  }
}
</script>

2.使用自定義指令

Vue.directive('getFocus', {
    inserted: function(el, binding) {
        el.focus()
    }
})

<input type="text" v-model="name" v-getFocus />

3.使用原生js

<input type="text" v-model="name" id="getFocus" />

<script>
export default {
  data() {
    return {
      name: ''
    }
  },
  mounted() {
      document.getElementById('getFocus').focus()
  }
}
</script>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM