【问题标题】:Handling keydown in firefox is failing在 Firefox 中处理 keydown 失败
【发布时间】:2018-05-02 07:13:51
【问题描述】:

我正在尝试在我的反应组件中覆盖页面的本机打印操作。 我添加了这些行:

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 库触发打印我的组件的打印友好版本的内容。 我错过了什么?

【问题讨论】:

    标签: javascript reactjs printing


    【解决方案1】:

    在firefox官方论坛上阅读以下说明:

    显然 Firefox 根本不在乎。

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

    在 Chrome 中停止页面保存选项,但在 Firefox 中无效 网页或扩展页面。

    打开一个新窗口可能不能也不应该被阻止(但是 从面板继承大小是愚蠢的,事实上 Firefox 会打开使用 browser.tabs.create({ }) 创建的新标签页 默认弹出窗口)。

    This is the link

    【讨论】:

      猜你喜欢
      • 2015-10-16
      • 1970-01-01
      • 1970-01-01
      • 2016-06-30
      • 2014-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-15
      相关资源
      最近更新 更多