簡體   English   中英

如何使用javascript從div獲取打印的選定內容?

[英]How to take print selected content from div using javascript?

我在div區域中有一些內容。 我想從div中打印出所選內容。 在我的文章中

例如,如果我從div內容中選擇3行 ,則在打印輸出后 ,我將3行內容作為單行獲得。 不是3行。

我的密碼

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"> 
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://richfaces.org/rich" prefix="rich" %>
<%@ taglib uri="http://richfaces.org/a4j" prefix="a4j"%>

<f:view>
    <html>
        <head>
           <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">


            function printPartialContent(divContentId)
            {
                var selectedContent = "";
                selectedContent= document.getSelection();

                var textPopupWindow = window.open('','popup','toolbar=no,menubar=no,width=1,height=1 left=-5000');
                textPopupWindow.document.open();
                textPopupWindow.document.write(makeTextPrint(selectedContent));
                textPopupWindow.document.close();
            }

            function makeTextPrint(textContent)
            {
                // We break the closing script tag in half to prevent
                // the HTML parser from seeing it as a part of
                // the *main* page.

                return  "<html>\n" +
                    "<head>\n" +
                    "<script>\n" +
                    "function step1() {\n" +
                    "  setTimeout('step2()', 10);\n" +
                    "}\n" +
                    "function step2() {\n" +
                    "  window.print();\n" +
                    "  window.close();\n" +
                    "}\n" +
                    "</scr" + "ipt>\n" +
                    "</head>\n" +
                    "<body onLoad='step1()'>\n" +
                    textContent +
                    "</body>\n" +
                    "</html>\n";
            }

        </script>
    </head>
 <body>
        <h:form id="parameterform">

            <rich:panel id="divPanelPanel">

                <f:facet name="header">
                    <h:outputText value="Div Content Demo"/>
                </f:facet>

                <a4j:outputPanel id="divOutputPanel">


                    <div id="contentDivId" style="text-align:left">

                        ******* Details 1 *******<br>
                        28/02/2011 08.19.00<br>
                        5641620100333718 566<br>
                        Value: 2000<br>
                        Counter : 123<br>  
                        ******* Details 2 *******<br>
                        16/10/2000 9.10.00<br>
                        56345345333718<br>
                        Value: 3000<br>
                        Counter : 765<br>                          
                    </div>

<a4j:commandButton value="Partial Content" 
onclick="printPartialContent('contentDivId')"/>


                 </a4j:outputPanel>

            </rich:panel>
</h:form
</body> </html></f:view>

幫我。 提前致謝

document.getSelection()將選擇的文本作為純文本而不是HTML返回,因此,當您將選擇內容輸出到打印窗口/ div時,所有格式都將丟失。 要將文本復制為HTML,請在跨平台的瀏覽器中簽出所選文本的HTML獲取所選文本

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM