element-ui vue input輸入框自動(dòng)獲取焦點(diǎn)聚焦方式
element-ui vue input輸入框自動(dòng)獲取焦點(diǎn)聚焦
有時(shí)候會(huì)遇到要輸入框自動(dòng)獲取焦點(diǎn)的情況,解決如下:
方法一
步驟:
1.在script中寫(xiě)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();
vue輸入框自動(dòng)獲取焦點(diǎn)的三種方式
方式一:原生JS操作DOM
<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>
方式二:ref方式實(shí)現(xiàn)
<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>
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- vue如何動(dòng)態(tài)獲取當(dāng)前時(shí)間
- vue怎樣獲取當(dāng)前時(shí)間,并且傳遞給后端(不用注解)
- vue 使用moment獲取當(dāng)前時(shí)間及一月前的時(shí)間
- Vue 中如何利用 new Date() 獲取當(dāng)前時(shí)間
- 基于vue實(shí)現(xiàn)8小時(shí)帶刻度的時(shí)間軸根據(jù)當(dāng)前時(shí)間實(shí)時(shí)定位功能
- vue中實(shí)現(xiàn)當(dāng)前時(shí)間echarts圖表時(shí)間軸動(dòng)態(tài)的數(shù)據(jù)(實(shí)例代碼)
- Vue 按照創(chuàng)建時(shí)間和當(dāng)前時(shí)間顯示操作(剛剛,幾小時(shí)前,幾天前)
- Vue 中獲取當(dāng)前時(shí)間并實(shí)時(shí)刷新的實(shí)現(xiàn)代碼
- Vue filter 過(guò)濾當(dāng)前時(shí)間 實(shí)現(xiàn)實(shí)時(shí)更新效果
- vue 2.1.3 實(shí)時(shí)顯示當(dāng)前時(shí)間,每秒更新的方法
- Vue3新增時(shí)自動(dòng)獲取當(dāng)前時(shí)間的操作方法
相關(guān)文章
vue中實(shí)現(xiàn)滾動(dòng)加載更多的示例
下面小編就為大家?guī)?lái)一篇vue中實(shí)現(xiàn)滾動(dòng)加載更多的示例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11Vue組件傳值異步問(wèn)題子組件拿到數(shù)據(jù)較慢解決
這篇文章主要為大家介紹了Vue組件傳值異步中子組件拿到數(shù)據(jù)較慢的問(wèn)題解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08Vue處理循環(huán)數(shù)據(jù)流程示例精講
這篇文章主要介紹了Vue處理循環(huán)數(shù)據(jù)流程,這個(gè)又是一個(gè)編程語(yǔ)言,?模版語(yǔ)法里面必不可少的一個(gè),?也是使用業(yè)務(wù)場(chǎng)景使用最多的一個(gè)環(huán)節(jié)。所以學(xué)會(huì)使用循環(huán)也是重中之重了2023-04-04vue實(shí)現(xiàn)定時(shí)刷新數(shù)據(jù),每隔5分鐘執(zhí)行一次
這篇文章主要介紹了vue實(shí)現(xiàn)定時(shí)刷新數(shù)據(jù),每隔5分鐘執(zhí)行一次問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01vue項(xiàng)目部署跨域問(wèn)題的詳細(xì)解決過(guò)程
一般我們的前端Vue項(xiàng)目中都會(huì)涉及到跨域的問(wèn)題,下面這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目部署跨域問(wèn)題的詳細(xì)解決過(guò)程,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05vue 解除鼠標(biāo)的監(jiān)聽(tīng)事件的方法
這篇文章主要介紹了vue 解除鼠標(biāo)的監(jiān)聽(tīng)事件的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11