gpt4 book ai didi

javascript - 在 Firefox 中处理 keydown 失败

转载 作者:行者123 更新时间:2023-11-30 20:33:06 27 4
gpt4 key购买 nike

我试图在我的 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com