Vue中keyup.enter和blur事件沖突的問(wèn)題及解決
keyup.enter和blur事件沖突問(wèn)題
<el-input class="input-new-tag" v-if="row.inputVisible" v-model="row.inputValue" :ref="$index" size="small" @keyup.enter.native="handleInputConfirm(row,$index)" @blur="handleInputConfirm(row,$index)" >
在寫(xiě)這個(gè)業(yè)務(wù)時(shí),遇到了一個(gè)回車和blur沖突的問(wèn)題,按了回車,導(dǎo)致了input也算失去了焦點(diǎn),導(dǎo)致連續(xù)觸發(fā)2次handleInputConfirm(row,$index)這個(gè)函數(shù)。
解決方法
<el-input class="input-new-tag" v-if="row.inputVisible" v-model="row.inputValue" :ref="$index" size="small" @keyup.enter.native="$event.target.blur" @blur="handleInputConfirm(row,$index)" > //將回車觸發(fā)的方法改為去觸發(fā)元素的blur事件 這樣就不會(huì)重復(fù)觸發(fā)了。
keyup.enter和blur同時(shí)觸發(fā)如何規(guī)避
問(wèn)題描述
在某種場(chǎng)景下,需要點(diǎn)擊span標(biāo)簽變成input標(biāo)簽,然后在input標(biāo)簽下編輯,編輯完成之后按回車或點(diǎn)擊input標(biāo)簽外的地方又變回span標(biāo)簽
雙擊后:
回車:
實(shí)際上觸發(fā)了兩次
實(shí)現(xiàn)代碼
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script> </head> <body> <div id="app"> <template v-if="isShow"> <span @dblclick="showInput">{{msg}}</span> </template> <template v-else> <input @keyup.enter="hideInput" @blur="hideInput" v-focus type="text" v-model="msg" /> </template> </div> <script> let app = new Vue({ el: '#app', data: { msg: 'hello', isShow: true, }, directives: { focus: { inserted: (el) => { el.focus() }, }, }, methods: { showInput() { this.isShow = false }, hideInput() { console.log('觸發(fā)') this.isShow = true }, }, }) </script> </body> </html>
解決辦法
@keyup.enter="$event.target.blur()"
修改后:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script> </head> <body> <div id="app"> <template v-if="isShow"> <span @dblclick="showInput">{{msg}}</span> </template> <template v-else> <input @keyup.enter="$event.target.blur()" @blur="hideInput" v-focus type="text" v-model="msg" /> </template> </div> <script> let app = new Vue({ el: '#app', data: { msg: 'hello', isShow: true, }, directives: { focus: { inserted: (el) => { el.focus() }, }, }, methods: { showInput() { this.isShow = false }, hideInput() { console.log('觸發(fā)') this.isShow = true }, }, }) </script> </body> </html>
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue實(shí)現(xiàn)滑動(dòng)和滾動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)滑動(dòng)和滾動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03Vue?axios和vue-axios的關(guān)系及使用區(qū)別
axios是基于promise的HTTP庫(kù),可以使用在瀏覽器和node.js中,它不是vue的第三方插件,vue-axios是axios集成到Vue.js的小包裝器,可以像插件一樣安裝使用:Vue.use(VueAxios, axios),本文給大家介紹Vue?axios和vue-axios關(guān)系,感興趣的朋友一起看看吧2022-08-08基于vue3&element-plus的暗黑模式實(shí)例詳解
實(shí)現(xiàn)暗黑主題的方式有很多種,也有很多成型的框架可以直接使用,下面這篇文章主要給大家介紹了關(guān)于基于vue3&element-plus的暗黑模式的相關(guān)資料,需要的朋友可以參考下2022-12-12在 Typescript 中使用可被復(fù)用的 Vue Mixin功能
這篇文章主要介紹了在 Typescript 中使用可被復(fù)用的 Vue Mixin功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-04-04詳解如何使用Object.defineProperty實(shí)現(xiàn)簡(jiǎn)易的vue功能
這篇文章主要為大家介紹了如何使用Object.defineProperty實(shí)現(xiàn)簡(jiǎn)易的vue功能示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04vue如何根據(jù)不同的環(huán)境使用不同的接口地址
這篇文章主要介紹了vue如何根據(jù)不同的環(huán)境使用不同的接口地址問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04Vue3+Vite實(shí)現(xiàn)項(xiàng)目搭建步驟
這篇文章主要介紹了Vue3+Vite實(shí)現(xiàn)項(xiàng)目搭建步驟,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07vue.js實(shí)現(xiàn)開(kāi)關(guān)(switch)組件實(shí)例代碼
這篇文章介紹了vue.js實(shí)現(xiàn)開(kāi)關(guān)(switch)組件的實(shí)例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06vue組件編寫(xiě)之todolist組件實(shí)例詳解
這篇文章主要介紹了vue組件編寫(xiě)之todolist組件的實(shí)例講解,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2018-01-01