亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在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 關注
  • 1010 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號