window.matchMedia('print') 在 Firefox 和 IE 中失败 [英] window.matchMedia('print') failing in Firefox and IE
问题描述
我有一个打印按钮,可以在任何网页上启动打印功能.一旦用户单击该按钮,该按钮就会隐藏,并在打印窗口中显示用户是否已完成打印或按下关闭键.它在 Chrome 中运行良好,但在 Firefox 和 IE 中失败.
I have a print button that launches the print functionality on any webpage. The button hides as soon as the user clicks on it and shows if the user is done printing or presses close in the print window. It works fine in Chrome but is failing in firefox and IE.
<input type="button" onclick="launchPrint()" value= "Print me" />
function launchPrint(){
$(".print-box").hide();
window.print();
}
(function() {
if (window.matchMedia) {
var mediaQueryList = window.matchMedia('print');
mediaQueryList.addListener(function(mql) {
if (!mql.matches) {
$(".print-box").show();
}
});
}
}());
有什么我可能遗漏的建议吗?
Any suggestions what I may be missing?
推荐答案
不幸的是,我和你遇到了同样的问题,我做了一些研究.目前看来,FF 和 IE 的最新版本仍然存在该错误,并且尚未修复.
Unfortunately, I am on the same problem as you and I did some research. For now it seems that the bug exists on recents version of FF and IE still and it hasn't been fixed.
您可以查看 Firefox 的此错误:https://bugzilla.mozilla.org/show_bug.cgi?id=774398
You can check out this bug for Firefox: https://bugzilla.mozilla.org/show_bug.cgi?id=774398
我发现另一个人和我们有同样的问题,但没有一个真正令人满意的答案:https://social.technet.microsoft.com/Forums/office/en-US/bc3ca05e-b4ef-425b-8bbd-d3f700a8c85e/windowmatchmedia-not-firing-for-print-in-ie?forum=ieitprocurrentver
I found another person having the same issue as us and it hasn't a really satisfying answer: https://social.technet.microsoft.com/Forums/office/en-US/bc3ca05e-b4ef-425b-8bbd-d3f700a8c85e/windowmatchmedia-not-firing-for-print-in-ie?forum=ieitprocurrentver
如果我遇到任何解决方案,我会编辑它.
If I ever come accross any solution, I will edit this.
我主要使用与您相同的代码作为 highchart 在打印前调整大小的示例:
I mostly use the same code as you as exemple for highchart to resize before printing:
function printUpdate() {
jQuery.each(Highcharts.charts, function(index, value){
value.reflow();
});
};
var mediaQueryList = window.matchMedia('print');
if(navigator.appVersion.indexOf("MSIE 8.")!==-1)//For IE8
{
document.attachEvent(function (mql){printUpdate();}, mediaQueryList);
}
else
{
if (window.matchMedia) {
mediaQueryList.addListener(function (mql) {
if (mql.matches)
{
printUpdate();
}
});
}
}
window.onbeforeprint = printUpdate;
这在 chrome 中运行良好.但是 FF 和 IE11 不会触发该事件.
This work fine in chrome. But FF and IE11 won't fire the event.
这篇关于window.matchMedia(&#39;print&#39;) 在 Firefox 和 IE 中失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!