您好,登錄后才能下訂單哦!
本文小編為大家詳細(xì)介紹“element-ui vue input輸入框自動(dòng)獲取焦點(diǎn)聚焦怎么實(shí)現(xiàn)”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“element-ui vue input輸入框自動(dòng)獲取焦點(diǎn)聚焦怎么實(shí)現(xiàn)”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識(shí)吧。
有時(shí)候會(huì)遇到要輸入框自動(dòng)獲取焦點(diǎn)的情況,解決如下:
步驟:
1.在script中寫directives,注冊(cè)一個(gè)全局的自定義指定 v-focus
directives: { focus: { inserted: function(el) { el.querySelector("input").focus(); } } },
2.在input框直接使用
<el-input ... v-focus > </el-input>
步驟:
1.給輸入框設(shè)置一個(gè)ref
<el-input ref="saveTagInput" >
2.在需要的時(shí)候操作ref獲取焦點(diǎn)
this.$refs.saveTagInput.focus();
<template> <div class="focusDemo"> <input type="text" v-model="username" id='inputId'/> </div> </template> <script> export default { data () { return { username: '' } }, mounted () { document.getElementById('inputId').focus() } } </script>
<template> <div class="focusDemo"> <input ref="inputName" type="text" v-model="username" /> </div> </template> <script> export default { data () { return { username: '' } }, mounted () { this.$nextTick(() => { this.$refs.inputName.focus() }) } } </script>
main.js中
// 注冊(cè)一個(gè)全局自定義指令 `v-focus` Vue.directive('focus', { // 當(dāng)被綁定的元素插入到 DOM 中時(shí) inserted: function (el) { // 聚焦元素 el.focus() }, update: function (el) { // 聚焦元素 el.focus() } })
vue文件中
<template> <div class="focusDemo"> <input type="text" v-model="username" v-focus /> </div> </template> <script> export default { data () { return { username: '' } } } </script>
讀到這里,這篇“element-ui vue input輸入框自動(dòng)獲取焦點(diǎn)聚焦怎么實(shí)現(xiàn)”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。