Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/35.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 打印<;div id=";printarea"&燃气轮机</部门>;只有_Javascript_Css_Printing_Dhtml - Fatal编程技术网

Javascript 打印<;div id=";printarea"&燃气轮机</部门>;只有

Javascript 打印<;div id=";printarea"&燃气轮机</部门>;只有,javascript,css,printing,dhtml,Javascript,Css,Printing,Dhtml,如何打印指定的div(而不手动禁用页面上的所有其他内容) 我希望避免出现新的预览对话框,因此使用此内容创建新窗口是没有用的 该页面包含两个表,其中一个表包含我要打印的div-该表的样式为web视觉样式,不应显示在打印中。您可以使用,并使用CSS来排列您要打印的内容吗?有关更多指针。hm。。。使用样式表的类型进行打印。。。例如: <link rel="stylesheet" type="text/css" href="print.css" media="print" /> 使用特殊样

如何打印指定的div(而不手动禁用页面上的所有其他内容)

我希望避免出现新的预览对话框,因此使用此内容创建新窗口是没有用的


该页面包含两个表,其中一个表包含我要打印的div-该表的样式为web视觉样式,不应显示在打印中。

您可以使用,并使用CSS来排列您要打印的内容吗?有关更多指针。

hm。。。使用样式表的类型进行打印。。。例如:

<link rel="stylesheet" type="text/css" href="print.css" media="print" />

使用特殊样式表进行打印

<link rel="stylesheet" href="print.css" type="text/css" media="print" />

您可以使用一个单独的CSS样式来禁用除id为“printarea”的内容之外的所有其他内容


有关更多说明和示例,请参见。

对于CSS 3,您可以使用以下内容:

body *:not(#printarea) {
    display: none;
}

到目前为止,所有的答案都有相当大的缺陷——要么将
class=“noprint”
添加到所有内容中,要么将
显示在
#printable

我认为最好的解决方案是围绕不可打印的内容创建一个包装:

<head>
    <style type="text/css">

    #printable { display: none; }

    @media print
    {
        #non-printable { display: none; }
        #printable { display: block; }
    }
    </style>
</head>
<body>
    <div id="non-printable">
        Your normal page contents
    </div>

    <div id="printable">
        Printer version
    </div>
</body>

#可打印{显示:无;}
@媒体印刷品
{
#不可打印{显示:无;}
#可打印{显示:块;}
}
您的正常页面内容
打印机版本

当然,这并不完美,因为它需要在HTML中移动一些内容…

如果您只想打印此div,则必须使用以下说明:

@media print{
    *{display:none;}
    #mydiv{display:block;}
}

我使用JavaScript获取了内容,并创建了一个可以打印的窗口来代替

  • 给出要打印id的任何元素
    printMe

  • 将此脚本包括在头标记中:

    <script language="javascript">
        var gAutoPrint = true;
    
        function processPrint(){
    
        if (document.getElementById != null){
        var html = '<HTML>\n<HEAD>\n';
        if (document.getElementsByTagName != null){
        var headTags = document.getElementsByTagName("head");
        if (headTags.length > 0) html += headTags[0].innerHTML;
        }
    
        html += '\n</HE' + 'AD>\n<BODY>\n';
        var printReadyElem = document.getElementById("printMe");
    
        if (printReadyElem != null) html += printReadyElem.innerHTML;
        else{
        alert("Error, no contents.");
        return;
        }
    
        html += '\n</BO' + 'DY>\n</HT' + 'ML>';
        var printWin = window.open("","processPrint");
        printWin.document.open();
        printWin.document.write(html);
        printWin.document.close();
    
        if (gAutoPrint) printWin.print();
        } else alert("Browser not supported.");
    
        }
    </script>
    
    
    var gAutoPrint=真;
    函数processPrint(){
    if(document.getElementById!=null){
    var html='\n\n';
    if(document.getElementsByTagName!=null){
    var headTags=document.getElementsByTagName(“head”);
    如果(headTags.length>0)html+=headTags[0].innerHTML;
    }
    html+='\n\n';
    var printReadyElem=document.getElementById(“printMe”);
    如果(printReadyElem!=null)html+=printReadyElem.innerHTML;
    否则{
    警报(“错误,无内容”);
    返回;
    }
    html+='\n\n';
    var printWin=window.open(“,“processPrint”);
    printWin.document.open();
    printWin.document.write(html);
    printWin.document.close();
    if(gAutoPrint)printWin.print();
    }else警报(“不支持浏览器”);
    }
    
  • 调用函数

    <a href="javascript:void(processPrint());">Print</a>
    
    
    

  • 桑德罗的方法非常有效

    我对它进行了调整,允许多个printMe链接,特别是在选项卡式页面和扩展文本中使用


    函数processPrint(printMe){
    使用jQuery,就这么简单:

    w=window.open();
    w.document.write($('.report_left_inner').html());
    w.print();
    w.close();
    

    这里是一个通用的解决方案,仅使用CSS,我已经验证了它的有效性

    @media print {
      body * {
        visibility: hidden;
      }
      #section-to-print, #section-to-print * {
        visibility: visible;
      }
      #section-to-print {
        position: absolute;
        left: 0;
        top: 0;
      }
    }
    
    其他方法不太好。使用
    display
    很棘手,因为如果任何元素具有
    display:none
    ,则其子元素也不会显示。要使用它,您必须更改页面的结构

    使用
    visibility
    效果更好,因为您可以打开子体的可见性。但不可见元素仍然会影响布局,因此我将
    部分移到左上角打印
    ,以便正确打印。

    printDiv(divId):打印任何页面上任何div的通用解决方案

    我有一个类似的问题,但我希望(a)能够打印整个页面,或(b)打印几个特定区域中的任何一个。我的解决方案,多亏了上面的许多内容,允许您指定要打印的任何div对象

    此解决方案的关键是在打印介质样式表中添加适当的规则,以便打印请求的div(及其内容)

    首先,创建所需的打印css以抑制所有内容(但没有允许打印元素的特定规则)

    第二个完成实际工作,第三个完成清理。第二个(printDiv)激活打印介质样式表,然后附加新规则以允许所需的div打印,发布打印,然后在最终整理之前添加延迟(否则,可以在实际完成打印之前重置样式)

    最终功能删除添加的规则,并再次将打印样式设置为禁用,以便可以打印整个页面

    function printDivRestore()
    {
      // remove the div-specific rule
      var sheetObj=document.styleSheets[0];  
      if (sheetObj.rules) { sheetObj.removeRule(sheetObj.rules.length-1); }
      else                { sheetObj.deleteRule(sheetObj.cssRules.length-1); }
      //  and re-enable whole page printing
      disableSheet(0,true);
    }
    
    唯一要做的另一件事是在onload处理中添加一行,以便最初禁用打印样式,从而允许整页打印

    <body onLoad='disableSheet(0,true)'>
    
    
    
    然后,您可以从文档中的任何位置打印div。只需通过按钮或其他方式发出printDiv(“thediv”)

    这种方法的一大优点是它提供了一种从页面中打印选定内容的通用解决方案。它还允许对打印的元素(包括包含div)使用现有样式


    注意:在我的实现中,这必须是第一个样式表。如果需要在以后的工作表序列中将工作表引用(0)更改为适当的工作表编号。

    我有一个更好的解决方案,代码最少

    将可打印部件放置在具有如下id的div中:

    <div id="printableArea">
          <h1>Print me</h1>
    </div>
    
    <input type="button" onclick="printDiv('printableArea')" value="print a div!" />
    
     <div style="display:none">
       <div id="modal-2" class="printableArea">
         <input type="button" class="printdiv-btn" value="print a div!" />
       </div>
     </div>
    
    注意这有多简单?没有弹出窗口,没有新窗口,没有疯狂的样式,没有像jquery这样的JS库。真正复杂的解决方案的问题(答案并不复杂,也不是我所指的)事实上,它永远不会在所有浏览器中转换!如果您想使样式不同,请按照选中的答案中所示,将媒体属性添加到样式表链接(media=“print”)

    没有绒毛,重量轻,只起作用。

    您可以使用:

    或者将
    visibility:visible
    visibility:hidden
    css属性与
    @media print{}
    function printDiv(divId) { // Enable the print CSS: (this temporarily disables being able to print the whole page) disableSheet(0,false); // Get the print style sheet and add a new rule for this div var sheetObj=document.styleSheets[0]; var showDivCSS="visibility:visible;display:block;position:absolute;top:30px;left:30px;"; if (sheetObj.rules) { sheetObj.addRule("#"+divId,showDivCSS); } else { sheetObj.insertRule("#"+divId+"{"+showDivCSS+"}",sheetObj.cssRules.length); } print(); // need a brief delay or the whole page will print setTimeout("printDivRestore()",100); }
    function printDivRestore()
    {
      // remove the div-specific rule
      var sheetObj=document.styleSheets[0];  
      if (sheetObj.rules) { sheetObj.removeRule(sheetObj.rules.length-1); }
      else                { sheetObj.deleteRule(sheetObj.cssRules.length-1); }
      //  and re-enable whole page printing
      disableSheet(0,true);
    }
    
    <body onLoad='disableSheet(0,true)'>
    
    <div id="printableArea">
          <h1>Print me</h1>
    </div>
    
    <input type="button" onclick="printDiv('printableArea')" value="print a div!" />
    
    function printDiv(divName) {
         var printContents = document.getElementById(divName).innerHTML;
         var originalContents = document.body.innerHTML;
    
         document.body.innerHTML = printContents;
    
         window.print();
    
         document.body.innerHTML = originalContents;
    }
    
    <script language="javascript">
    function PrintMe(DivID) {
    var disp_setting="toolbar=yes,location=no,";
    disp_setting+="directories=yes,menubar=yes,";
    disp_setting+="scrollbars=yes,width=650, height=600, left=100, top=25";
       var content_vlue = document.getElementById(DivID).innerHTML;
       var docprint=window.open("","",disp_setting);
       docprint.document.open();
       docprint.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"');
       docprint.document.write('"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">');
       docprint.document.write('<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">');
       docprint.document.write('<head><title>My Title</title>');
       docprint.document.write('<style type="text/css">body{ margin:0px;');
       docprint.document.write('font-family:verdana,Arial;color:#000;');
       docprint.document.write('font-family:Verdana, Geneva, sans-serif; font-size:12px;}');
       docprint.document.write('a{color:#000;text-decoration:none;} </style>');
       docprint.document.write('</head><body onLoad="self.print()"><center>');
       docprint.document.write(content_vlue);
       docprint.document.write('</center></body></html>');
       docprint.document.close();
       docprint.focus();
    }
    </script>
    
    <input type="button" name="btnprint" value="Print" onclick="PrintMe('divid')"/>
    <div id="divid">
    here is some text to print inside this div with an id 'divid'
    </div>
    
    body > *:not(.printableArea) {
        display: none;
    }
    
    //Not needed if already showing
    body > .printableArea {
        display: block;
    }
    
    body > *:not(.parentDiv),
    .parentDiv > *:not(.printableArea) {
        display: none;
    }
    
    //Not needed if already showing
    body > .printableArea {
        display: block;
    }
    
    <style type="text/css">
    @media print {
       //styles here
    }
    </style>
    
    <link rel="stylesheet" type="text/css" media="print" href="print.css" />
    
    <script type="text/javascript">
       function printDiv(divId) {
           var printContents = document.getElementById(divId).innerHTML;
           var originalContents = document.body.innerHTML;
           document.body.innerHTML = "<html><head><title></title></head><body>" + printContents + "</body>";
           window.print();
           document.body.innerHTML = originalContents;
       }
    </script>
    
    function printDiv(divName){
    
        var printContents = document.getElementById(divName).innerHTML;
    
        document.getElementById("body_print").innerHTML = printContents;
    
        document.getElementById("body_allin").style.display = "none";
        document.getElementById("body_print").style.display = "";
    
        window.print();
    
        document.getElementById("body_print").innerHTML = "";
        document.getElementById("body_allin").style.display = "";
        document.getElementById("body_print").style.display = "none";
    
    }
    
     <div style="display:none">
       <div id="modal-2" class="printableArea">
         <input type="button" class="printdiv-btn" value="print a div!" />
       </div>
     </div>
    
    $(document).on('click', '.printdiv-btn', function(e) {
        e.preventDefault();
    
        var $this = $(this);
        var originalContent = $('body').html();
        var printArea = $this.parents('.printableArea').html();
    
        $('body').html(printArea);
        window.print();
        $('body').html(originalContent);
    });
    
    @media print {
      body * {
        visibility: hidden;
      }
    
      #not-print * {
        display: none;
      }
    
      #to-print, #to-print * {
        visibility: visible;
      }
    
      #to-print {
        display: block !important;
        position: absolute;
        left: 0;
        top: 0;
        width: auto;
        height: 99%;
      }
    }
    
    <div id="not-print" >
      <header class="row wrapper page-heading">
    
      </header>
    
      <div class="wrapper wrapper-content">
        <%= image_tag @qrcode.image_url,  size: "250x250" , alt: "#{@qrcode.name}" %>
      </div>
    </div>
    
    <div id="to-print" style="display: none;">
      <%= image_tag @qrcode.image_url,  size: "300x300" , alt: "#{@qrcode.name}" %>
    </div>
    
            function printDiv(divName) {
    
            var printContents = document.getElementById(divName).innerHTML;
            w = window.open();
    
            w.document.write(printContents);
            w.document.write('<scr' + 'ipt type="text/javascript">' + 'window.onload = function() { window.print(); window.close(); };' + '</sc' + 'ript>');
    
            w.document.close(); // necessary for IE >= 10
            w.focus(); // necessary for IE >= 10
    
            return true;
        }
    
    <div id="printableArea">
          <h1>Print me</h1>
    </div>
    
    <input type="button" onclick="printDiv('printableArea')" value="print a div!" />
    
    <div id='printarea'>
        <p>This is a sample text for printing purpose.</p>
        <input type='button' id='btn' value='Print' onclick='printFunc();'>
    </div>
    <p>Do not print.</p>
    
    function printFunc() {
        var divToPrint = document.getElementById('printarea');
        var htmlToPrint = '' +
            '<style type="text/css">' +
            'table th, table td {' +
            'border:1px solid #000;' +
            'padding;0.5em;' +
            '}' +
            '</style>';
        htmlToPrint += divToPrint.outerHTML;
        newWin = window.open("");
        newWin.document.write("<h3 align='center'>Print Page</h3>");
        newWin.document.write(htmlToPrint);
        newWin.print();
        newWin.close();
        }
    
    printWindow(selector, title) {
       var divContents = $(selector).html();
       var $cssLink = $('link');
       var printWindow = window.open('', '', 'height=' + window.outerHeight * 0.6 + ', width=' + window.outerWidth  * 0.6);
       printWindow.document.write('<html><head><h2><b><title>' + title + '</title></b></h2>');
       for(var i = 0; i<$cssLink.length; i++) {
        printWindow.document.write($cssLink[i].outerHTML);
       }
       printWindow.document.write('</head><body >');
       printWindow.document.write(divContents);
       printWindow.document.write('</body></html>');
       printWindow.document.close();
       printWindow.onload = function () {
                printWindow.focus();
                setTimeout( function () {
                    printWindow.print();
                    printWindow.close();
                }, 100);  
            }
    }
    
    @media print {
      body * {
        visibility: hidden;
        height:0;
      }
      #section-to-print, #section-to-print * {
        visibility: visible;
        height:auto;
      }
      #section-to-print {
        position: absolute;
        left: 0;
        top: 0;
      }
    }
    
    @media print {
        .print-area {
            background-color: white;
            height: 100%;
            width: auto;
            position: absolute;
            top: 0;
            bottom: 0;
            left: 0;
            right: 0;
            z-index:1500;
            visibility: visible;
        }
        @page{
            size: portrait;
            margin: 1cm;
        }
    /*IF print-area parent element is position:absolute*/
        .ui-dialog,
        .ui-dialog .ui-dialog-content{
            position:unset !important;
            visibility: hidden;
        }
    }
    
    printDiv("myDiv");
    
    function printDiv(id){
            var printContents = document.getElementById(id).innerHTML;
            var originalContents = document.body.innerHTML;
            document.body.innerHTML = printContents;
            window.print();
            document.body.innerHTML = originalContents;
    }
    
    <div id="monitor" onclick="idElementPrint()">text i want to print</div>
    
    //or: monitor.textContent = "click me to print textual content";
    
    const idElementPrint = () => {
        let ifram = document.createElement("iframe");
        ifram.style = "display:none";
        document.body.appendChild(ifram);
        pri = ifram.contentWindow;
        pri.document.open();
        pri.document.write(monitor.textContent);
        pri.document.close();
        pri.focus();
        pri.print();
        }
    
    function PrintArea(selector) {
        //First hide all content in body.
        var all = $("body > header, body > #Content, body > footer")
        all.hide();
        //Append a div for printing.
        $("body").append('<div id="PrintMe">');
        //Copy content of printing area to the printing div.
        var p = $("#PrintMe");
        p.html($(selector).html());
        //Call the print dialog.
        window.print();
        //Remove the printing div.
        p.remove();
        //Show all content in body.
        all.show();
    }