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 {