简体   繁体   English

chrome无法使用event.preventDefault()

[英]unable to use event.preventDefault() with chrome

I am using ctrl + charCode**s in order to delete a row from the listing page which is working fine on firefox but when I tested it on **Chrome , the event.preventDefault() couldn't stop the browser based function to come up 我正在使用ctrl + charCode **来从列表页中删除一行,该行在firefox上运行良好,但是当我在** Chrome上对其进行测试时event.preventDefault()无法将基于浏览器的函数停止为上来

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

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

You can acheive with jQuery. 您可以使用jQuery实现。 Include jQuery library 包括jQuery库

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

This works :) 这有效:)

look this answer Override the bookmark shortcut (Ctrl+D) function in Chrome 看这个答案替代Chrome中的书签快捷键(Ctrl + D)功能

working solution just tested on this page 刚刚在此页面上测试过的有效解决方案

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM