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)"
>在寫這個(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)文章
vue3+ts+Vuex中使用websocket協(xié)議方式
這篇文章主要介紹了vue3+ts+Vuex中使用websocket協(xié)議方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
Vue.js每天必學(xué)之過(guò)濾器與自定義過(guò)濾器
Vue.js每天必學(xué)之過(guò)濾器與自定義過(guò)濾器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
Vue開(kāi)發(fā)手冊(cè)Function-based?API?RFC
這篇文章主要為大家介紹了Vue開(kāi)發(fā)手冊(cè)Function-based?API?RFC使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
vue+Element-ui實(shí)現(xiàn)分頁(yè)效果
這篇文章主要為大家詳細(xì)介紹了vue+Element-ui實(shí)現(xiàn)分頁(yè)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11
vue 支持百萬(wàn)量級(jí)的無(wú)限滾動(dòng)組件詳解
這篇文章主要為大家介紹了vue 支持百萬(wàn)量級(jí)的無(wú)限滾動(dòng)組件詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
vue中el-date-picker選擇日期的限制的項(xiàng)目實(shí)踐
ElementUI的el-date-picker使用時(shí),有時(shí)候想要限制用戶選擇的時(shí)間范圍,本文就來(lái)介紹了vue中el-date-picker選擇日期的限制的項(xiàng)目實(shí)踐,感興趣的可以了解一下2023-10-10
element-ui中el-form-item內(nèi)的el-select該如何自適應(yīng)寬度
自從用了element-ui,確實(shí)好用,該有的組件都有,但是組件間的樣式都固定好了,下面這篇文章主要給大家介紹了關(guān)于element-ui中el-form-item內(nèi)的el-select該如何自適應(yīng)寬度的相關(guān)資料,需要的朋友可以參考下2022-11-11

