2013-11-22 76 views
0

我正在使用jquery插件来打印我的网页的特定div。它在Firfox和IE中工作正常。但不幸的是,它不适用于Google Chrome。实际上谷歌浏览器正在打印包括横幅,菜单和按钮的完整网页。我无法解决这个问题。Google chrome打印问题

这里是我的示例代码

Java脚本

$(function() { 
    $("input:button").click(function() { 
     $("#print").printThis(); 
    }); 
}); 

HTML

<body> 
<input type="button" value="print" /> 
<div id="print"> 
this is print area, it can be everything. this is print area, it can be everything. this is print area, it can be everything. this is print area, it can be everything. this is 
</div> 
</body> 
+0

这将有助于知道您使用的是哪个插件。无论如何,在大多数情况下,使用CSS来定义可打印区域是最容易的,除非您打算在打印整个页面的同时提供单独的打印部分。 – JJJ

回答

0

您可以使用 “纯” 的jQuery做同样的:

//Begin browser detection section 
var isOpera = !! window.opera || navigator.userAgent.indexOf(' OPR/') >= 0; 
// Opera 8.0+ (UA detection to detect Blink/v8-powered Opera) 
var isFirefox = typeof InstallTrigger !== 'undefined'; // Firefox 1.0+ 
var isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0; 
// At least Safari 3+: "[object HTMLElementConstructor]" 
var isChrome = !! window.chrome && !isOpera; // Chrome 1+ 
var isIE = false || !! document.documentMode; // At least IE6 
    //End browser detection section 

    //User click on something 
$('a[href="#print"]').on("click", function() { 
    var data = "<div> a lot of HTML code from AJAX call or from your DIV </div>"; 
      CreateWindow(data); 
}); 


function CreateWindow(data) { 
    var mywindow = window.open('about:blank', '_blank'); 
    var myWindowContents = '<html>' + 
     '<head>' + 
     '<title>Results</title>' + 
     '<link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet" type="text/css">' + 
     '</head>' + 
     '<body >' + data + 
     '<script src="http://code.jquery.com/jquery-2.1.0.min.js type="text/javascript">' + '<' + '/script>' + 
     '<script type="text/javascript">' + 
     '$(document).ready(function() {' + 
     '});' + 
     '<' + '/script>' + 
     '</body>' + 
     '</html>'; 
    mywindow.document.write(myWindowContents); 
    mywindow.document.close(); 
    mywindow.focus(); 
    if (!isChrome) { 
     mywindow.print(); 
    } 
} 

您还可以看看我的代码在这里http://jsfiddle.net/pvkovalev/2pn4p/ 我认为它会给你一个想法如何解决您的问题。