Programmatically set focus on button in Vuetify

旧巷老猫 提交于 2019-12-12 11:43:01

问题


I am trying to focus the action buttons in a v-dialog every time it is opened. I tried using autofocus however it only works once.

Normally in Vuetify you can set the focus on an element by adding a reference and then calling the focus() function like this:

<v-text-field ref="refToElement" />

Code:

this.$nextTick(() => this.$refs.refToElement.focus())

However for v-btn this doesn't seem to work. How can I use javascript to focus the v-btn every time the dialog is shown?


回答1:


To set the focus on a v-btn you can add $el after the reference. For example

this.$nextTick(() => this.$refs.refToElement.$el.focus())

Full codepen of dialog and focused button: https://codepen.io/cby016/pen/eQXoBo.



来源:https://stackoverflow.com/questions/53597532/programmatically-set-focus-on-button-in-vuetify

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!