From 0511f6186e2df37cc053163d28f280f91c7884d8 Mon Sep 17 00:00:00 2001 From: zhaoxiao Date: Wed, 27 Oct 2021 18:19:40 +0800 Subject: [PATCH] =?UTF-8?q?[bug]=20backspace=E5=9B=9E=E9=80=80=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.vue | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/App.vue b/src/App.vue index 03a4ae8..bb0a332 100644 --- a/src/App.vue +++ b/src/App.vue @@ -18,8 +18,27 @@ window.addEventListener('message', function (event) { document.getElementsByClassName('tab-layout-tabs ant-tabs ant-tabs-top')[0].style.display = 'none' }, 1000) } - }) + +document.onkeydown = keyDown + +function keyDown(e) { + console.log(navigator.userAgent) + // 拦截backspace按钮,谷歌浏览器不需要拦截 + // 拦截仍然存在的问题:当input、textarea失去焦点后,仍然会后退页面 + if (e.keyCode === 8 && navigator.userAgent.indexOf('AppleWebKit') === -1 ) { + // 判断是不是在input或textarea触发backspace按钮事件 + if (window.event.srcElement.tagName.toUpperCase() === 'INPUT' || window.event.srcElement.tagName.toUpperCase() === 'TEXTAREA') { + // 判断输入框中是否还有值可删除 + if (window.event.srcElement.value.length === 1) { + // 拦截事件 + e.keyCode=0 + e.returnValue=false + } + } + } +} + export default { data() { return {