Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用jQuery打印div时,打印窗口在新选项卡中保持打开状态_Jquery - Fatal编程技术网

使用jQuery打印div时,打印窗口在新选项卡中保持打开状态

使用jQuery打印div时,打印窗口在新选项卡中保持打开状态,jquery,Jquery,我使用下面的代码使用jQuery打印一个div 我正在浏览器中打开一个新的选项卡(窗口)来打印它 其工作正常,但打印窗口(选项卡)在新选项卡中仍保持打开状态,该选项卡应在“取消”按钮上关闭 函数printDiv(id){ var html=“”; $('link')。每个(函数(){ if($(this.attr('rel').indexOf('stylesheet')!=-1){ html+=''; } }); html++=''+$(“#”+id).html()+''; var w=wind

我使用下面的代码使用jQuery打印一个div

我正在浏览器中打开一个新的选项卡(窗口)来打印它

其工作正常,但打印窗口(选项卡)在新选项卡中仍保持打开状态,该选项卡应在“取消”按钮上关闭

函数printDiv(id){
var html=“”;
$('link')。每个(函数(){
if($(this.attr('rel').indexOf('stylesheet')!=-1){
html+='';
}
});
html++=''+$(“#”+id).html()+'';
var w=window.open();
if(w){w.document.write(html);w.document.close()}
}
function printDiv(id) {
var html = "";

$('link').each(function () { 
    if ($(this).attr('rel').indexOf('stylesheet') != -1) { 
        html += '<link rel="stylesheet" href="' + $(this).attr("href") + '" />';
    }
});
html += '<body onload="window.focus(); window.print()">' + $("#" + id).html() + '</body>';
var w = window.open();   
if (w) { w.document.write(html); w.document.close() } 
}