Fire javascript function when ctrl+p is pressed

大城市里の小女人 提交于 2020-01-17 04:29:06

问题


I need to destroy a jQueryUI tabs when somebody tries to print the page. I can't hide it with CSS as I need the data from these tabs.

Can anyone help/point me in the right direction with this? Perhaps there are other ways of achieving the same results?

By destroy i mean:

$('#tabs').tabs("destroy");

This has to work on IE7/8, as that is the browser used in the company.

Solution (Thanks to @Phil ):

//Destroys the tabs for print
window.onbeforeprint = destroyTabs;

//Remakes tabs after printing
window.onafterprint = makeTabs;


function makeTabs() {
    $('#tabs').tabs();
}

function destroyTabs() {
    $('#tabs').tabs('destroy');
}

回答1:


This might work:

@media print {
    .ui-tabs-nav { display: none; }
    .ui-tabs .ui-tabs-hide { display: block !important; }
}

This is another shot in the dark (i haven't tried it) but:

<script type="text/javascript">
     window.onbeforeprint = destroyTabs;

     function destroyTabs(){
       $('#tabs').tabs('destroy').tabs();
     }
</script>



回答2:


There is no way to do this in JavaScript in a cross browser friendly way.

The onbeforeprint and onafterprint methods will only work in IE and Firefox 6+. You can combine them with window.matchMedia to add support for Chrome 9+ and Safari 5.1+. I've written about how to implement this at http://tjvantoll.com/2012/06/15/detecting-print-requests-with-javascript/.




回答3:


$(document).keypress("p",function(e) {
  if(e.ctrlKey)
    alert("Ctrl+P was pressed!!");
});


来源:https://stackoverflow.com/questions/11349129/fire-javascript-function-when-ctrlp-is-pressed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!