我搜索了很多,找到了一个完美的解决方案:)
使用onclick事件
function printCanvas()
{
var dataUrl = document.getElementById('anycanvas').toDataURL(); //attempt to save base64 string to server using this var
var windowContent = '';
windowContent += ''
windowContent += '
Print canvas';windowContent += '
'windowContent += '';
windowContent += '';
windowContent += '';
var printWin = window.open('','','width=340,height=260');
printWin.document.open();
printWin.document.write(windowContent);
printWin.document.close();
printWin.focus();
printWin.print();
printWin.close();
}