javascript - 在 Firefox 中处理 keydown 失败

标签 javascript reactjs printing

我试图在我的 React 组件中覆盖页面的 native 打印操作。 我添加了这些行:

componentWillMount() {
 document.addEventListener('keydown', this.handlePrintKeyDown.bind(this));
}

componentWillUnmount() {
 document.removeEventListener('keydown', this.handlePrintKeyDown.bind(this));
}

handlePrintKeyDown = event => {
 //(Cntrl + P)
 if (event.ctrlKey && event.keyCode === 80) {
  document.getElementById('printme').click();
  event.preventDefault();
  return false;
 }
};

它适用于 Chrome 和 Edge,但在 Firefox 中,它仍会尝试打开默认打印。 我必须补充一点,我想做的是打开一个窗口并使用 react-to-print 触发打印我的组件的打印友好版本的内容。图书馆。 我错过了什么?

最佳答案

在firefox官方论坛阅读以下解释:

Apparently Firefox just doesn’t care.

document.onkeydown = event => { console.log(event); event.preventDefault(); }

stops the page save option in Chrome, but has no effect in Firefox on either web or extension pages.

Opening a new window probably can’t and shouldn’t be prevented (but inheriting the size from the panel is stupid, as is the fact that Firefox opens new tabs created with browser.tabs.create({ }) inside the popup by default).

This is the link

关于javascript - 在 Firefox 中处理 keydown 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50129006/

相关文章:

html - CSS 打印布局 - 在单页上打印

java - 使用 javascript 仅打印 html 表格

javascript - 使用点击事件 javascript 创建简单的星级评定

javascript - 如何计算 JavaScript 对象的属性?

javascript - 类型错误 : Cannot read property 'length' of undefined in react

javascript - 我需要在 POST 请求后调用页面渲染

javascript - 如何调用文本框中的文本以在 javascript 中设置变量

javascript - 从脚本/命令行执行浏览器页面/javascript

reactjs - 当为组件提供唯一的键时,可以使用 Math.random() 来生成这些键吗?

c - 尝试打印结构元素,但结果为空白。 - C