如何在 Chrome 或 Firefox 中打印特定的 HTML 元素而不是整个页面?

f_f*_*ola 6 html printing pdf firefox google-chrome

假设我想打印(或转换为 PDF)div复杂 HTML 页面的特定区域(例如 a )。如何在 Chrome 或 Firefox 中做到这一点?我没有从“检查元素”功能中找到特定选项。

小智 0

试试这个。我也在 stackoverflow 中找到了这个:)

试试这个,这是解决您的问题的一个很好的例子

函数 PrintElem(elem) { Popup($(elem).html()); }

        function Popup(data) 
        {
            var mywindow = window.open('', 'my div', 'height=400,width=600');
            mywindow.document.write('<html><head><title>my div</title>');
            /*optional stylesheet*/ //mywindow.document.write('<link rel="stylesheet" href="main.css" type="text/css" />');
            mywindow.document.write('</head><body >');
            mywindow.document.write(data);
            mywindow.document.write('</body></html>');

            mywindow.document.close(); // necessary for IE >= 10
            mywindow.focus(); // necessary for IE >= 10

            mywindow.print();
            mywindow.close();

            return true;
        }

    </script>
</head>
<body>
    <div id="mydiv">
        This will be printed. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque a quam at nibh adipiscing interdum. Nulla vitae accumsan ante. 
    </div>
    <div>
        This will not be printed.
    </div>
    <div id="anotherdiv">
        Nor will this.
    </div>
    <input type="button" value="Print Div" onclick="PrintElem('#mydiv')" />
</body>
Run Code Online (Sandbox Code Playgroud)