为了账号安全,请及时绑定邮箱和手机立即绑定

如何在JavaScript中打印呈现的HTML页面的一部分?

如何在JavaScript中打印呈现的HTML页面的一部分?

蝴蝶不菲 2019-09-20 16:26:38
JavaScript代码window.print()可以打印当前的HTML页面。如果我在HTML页面中有一个div(例如,从ASP.NET MVC视图呈现的页面),那么我只想打印div。是否有任何jQuery 不引人注目的JavaScript或普通的JavaScript代码来实现此请求?更清楚地说,假设呈现的HTML页面如下:<html xmlns="http://www.w3.org/1999/xhtml">    <head id="Head" runat="server">        <title>            <asp:ContentPlaceHolder runat="server" ID="TitleContent" />        </title>    </head>    <body>            <div id="div1" class="div1">....</div>            <div id="div2" class="div2">....</div>            <div id="div3" class="div3">....</div>            <div id="div4" class="div4">....</div>            <div id="div4" class="div4">....</div>        <p>            <input id="btnSubmit" type="submit" value="Print" onclick="divPrint();" />        </p>    </body></html>然后我想点击打印按钮,只打印div3。
查看完整描述

3 回答

?
翻过高山走不出你

TA贡献1875条经验 获得超3个赞

我会有点像这样:


<html>

    <head>

        <title>Print Test Page</title>

        <script>

            printDivCSS = new String ('<link href="myprintstyle.css" rel="stylesheet" type="text/css">')

            function printDiv(divId) {

                window.frames["print_frame"].document.body.innerHTML=printDivCSS + document.getElementById(divId).innerHTML;

                window.frames["print_frame"].window.focus();

                window.frames["print_frame"].window.print();

            }

        </script>

    </head>


    <body>

        <h1><b><center>This is a test page for printing</center></b><hr color=#00cc00 width=95%></h1>

        <b>Div 1:</b> <a href="javascript:printDiv('div1')">Print</a><br>

        <div id="div1">This is the div1's print output</div>

        <br><br>

        <b>Div 2:</b> <a href="javascript:printDiv('div2')">Print</a><br>

        <div id="div2">This is the div2's print output</div>

        <br><br>

        <b>Div 3:</b> <a href="javascript:printDiv('div3')">Print</a><br>

        <div id="div3">This is the div3's print output</div>

        <iframe name="print_frame" width="0" height="0" frameborder="0" src="about:blank"></iframe>

    </body>

</html>


查看完整回答
反对 回复 2019-09-20
?
神不在的星期二

TA贡献1963条经验 获得超6个赞

试试这个JavaScript代码:


function printout() {


    var newWindow = window.open();

    newWindow.document.write(document.getElementById("output").innerHTML);

    newWindow.print();

}


查看完整回答
反对 回复 2019-09-20
  • 3 回答
  • 0 关注
  • 926 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信