【问题标题】:unable to use event.preventDefault() with chrome无法将 event.preventDefault() 与 chrome 一起使用
【发布时间】:2023-04-10 02:09:02
【问题描述】:

我正在使用 ctrl + charCode**s 从列表页面中删除一行,该行在 Firefox 上运行良好,但当我在 **Chrome 上测试时,事件.preventDefault() 无法阻止基于浏览器的功能出现

function showkey(e){
     if(e.ctrlKey && e.charCode == 100){  
        e.preventDefault();
        //delete code // }

<body onkeypress="showkey(event);">

【问题讨论】:

  • 也使用 e.stopPropagation() 来停止冒泡

标签: javascript ctrl


【解决方案1】:

您可以使用 jQuery 来实现。包含 jQuery 库

$(document).bind('keydown', function(e) {
  if(e.ctrlKey && (e.which == 100)) {
    e.preventDefault();
    return false;
  }
});

这行得通:)

【讨论】:

    【解决方案2】:

    看看这个答案 Override the bookmark shortcut (Ctrl+D) function in Chrome

    刚刚在此页面上测试过的工作解决方案

    document.addEventListener('keydown', function(event) {
      if (event.ctrlKey && String.fromCharCode(event.keyCode) === 'D') {
        console.log("you pressed ctrl-D");
        event.preventDefault();
        event.stopPropagation();
      }
    }, true);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-17
      • 2017-01-18
      • 1970-01-01
      • 1970-01-01
      • 2018-01-06
      • 2022-01-22
      • 2018-12-26
      相关资源
      最近更新 更多