Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/400.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/76.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.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
Javascript CSS不适用于打印文档_Javascript_Html - Fatal编程技术网

Javascript CSS不适用于打印文档

Javascript CSS不适用于打印文档,javascript,html,Javascript,Html,我正在使用以下代码打印文档: function print(html) { $('<iframe>', { name: 'myiframe', class: 'printFrame' }).appendTo('body').contents().find('body').append(html); window.frames['myiframe'].focus(); wi

我正在使用以下代码打印文档:

function print(html) {
        $('<iframe>', {
            name: 'myiframe',
            class: 'printFrame'
        }).appendTo('body').contents().find('body').append(html);

        window.frames['myiframe'].focus();
        window.frames['myiframe'].print();
        setTimeout(() => { $(".printFrame").remove(); }, 1000);
}

var html = '<head>' +
               '<link rel="stylesheet" href="/static/css/main.css" type="text/css" media="print">' +
               '<link rel="stylesheet" href="/static/css/vahan.css" type="text/css" media="print">' +
               '</head>' +
               '<body>' +
               '<div>Test</div>' +
               '</body>';
print(html);
函数打印(html){
$('', {
名称:“myiframe”,
类:“打印框”
}).appendTo('body').contents().find('body').append(html);
frames['myiframe'].focus();
frames['myiframe'].print();
setTimeout(()=>{$(“.printFrame”).remove();},1000);
}
var html=''+
'' +
'' +
'' +
'' +
“测试”+
'';
打印(html);

问题是我看到纯html没有应用任何css。肯定有一些简单的事情我想不出来。有什么提示吗?

在添加iframe之前,您需要先将html附加到文档中

function print(html) {
  // Add the html
  document.innerHTML = html

  // Append the iframe to the body
  $('<iframe>', {
    name: 'myiframe',
    class: 'printFrame'
  }).appendTo('body').contents();

  window.frames['myiframe'].focus();
  window.frames['myiframe'].print();
  setTimeout(() = > {
    $(".printFrame").remove();
  }, 1000);
}
函数打印(html){
//添加html
document.innerHTML=html
//将iframe附加到主体
$('', {
名称:“myiframe”,
类:“打印框”
}).appendTo('body').contents();
frames['myiframe'].focus();
frames['myiframe'].print();
设置超时(()=>{
$(“.printFrame”).remove();
}, 1000);
}

在将iframe附加到正文之前,需要在文档中放置
html