想把一個網頁上的部分內容寫到一個新開啟的窗口上,再把新開啟網頁上的內容直接打印出來,打印完畢候將新開啟的窗口關閉掉.
那位大哥給個範例.不勝感謝!!!!!
如一個頁面上的TABLE,DIV.寫到新網頁上再打印.

解决方案 »

  1.   

    1.htm
    <div id="div1">asdasd</div>
    <script defer>
    window.open("2.htm")
    </script>2.htm
    <script language=javascript defer>
       document.body.innerHTML=opener.document.getElementById("div1").innerHTML
       window.print();
       window.close();
    </script>
    有個朋友給出以上答案:
    我想要的效果如下:
    2.htm裡面的內容可以動態的調用1.htm中的內容.
    opener.document.getElementById("div1").innerHTML,這裡的div1不是寫死的.
    而是由先在1.htm中通過一個函數,將內容組在一塊.
    如:1.htm的內容如下:
    <table name="t1">
    <tr><td>姓名</td></tr>
    <tr><td>年齡</td></tr>
    </table>
    <table>
    <table>
    <tr><td>學校</td><td>打印1</td></tr>
    <tr></td>性別</td><td>打印2</td></tr>
    </table>
    如果我點"打印1"的時候,
    打出的內容是
    <table name="t1">
    <tr><td>姓名</td></tr>
    <tr><td>年齡</td></tr>
    <tr><td>學校</td><td>
    </table>
    當我點"打印2"的時候,打出的內容是
    <table name="t1">
    <tr><td>姓名</td></tr>
    <tr><td>年齡</td></tr>
    <tr><td>性別</td></tr>
    </table>  
      

  2.   

    你的需求太模糊,JS是肯定能办到的。问题是你哪些是固定的,哪些是动态的?既然是动态数据。那么为何不直接输出为javascript变量来处理?而要处理一个动态table?而且table不像DIV,涉及到单元格合并等处理。我给你写段代码,你参考一下。如果还解决不了。那你把你的需求再详细描述一遍:<html>
    <head>
    <title></title>
    <script type="text/javascript">function printNewWindow(tableObj, tdval){
    newWindow = window.open('','newWindow','height=300,width=500,scrollbars=auto');
    if (newWindow != null){
    var docbody = newWindow.document.createElement("body");
    newWindow.document.appendChild(docbody);
    docbody.innerHTML = tableObj.outerHTML; var tablebody = newWindow.document.createElement("tbody");
    docbody.getElementsByTagName("table")[0].appendChild(tablebody); var aTr = newWindow.document.createElement("tr");
    var aTd = newWindow.document.createElement("td");
    aTd.innerHTML = tdval;
    aTr.appendChild(aTd);
    tablebody.appendChild(aTr); newWindow.print();
    newWindow.close();
    }
    }</script>
    </head>
    <body>
    <table id="t1">
    <tbody>
    <tr><td>姓名</td></tr>
    <tr><td>年齡</td></tr>
    </tbody>
    </table>
    </table>
    <table>
    <tr>
    <td>學校</td>
    <td>
      <input type="button" name="Submit" value="打印1" onClick="printNewWindow(document.getElementById('t1'),this.parentNode.previousSibling.innerHTML)"></td>
    </tr>
    <tr>
    <td>性別</td>
    <td>
      <input type="button" name="Submit2" value="打印2" onClick="printNewWindow(document.getElementById('t1'),this.parentNode.previousSibling.innerHTML)"></td>
    </tr>
    </table></body>
    </html>
      

  3.   

    結合樓上的代碼,我的已經滿足需求了.
    還有個問題請教;
    當打印的時候新打開的窗口不回自動關閉.
    newWindow.close();沒有起作用,還是另有原因.
    當我把打印功能注釋掉的時候,窗口可以自動關閉.
      

  4.   

    通过设置display: none; 应该可以达到那样的效果.看看:http://sunflowerbbs.oicp.net/posts/list/47.page