我试图在我的 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).
关于javascript - 在 Firefox 中处理 keydown 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50129006/