日期:2014-05-20  浏览次数:20657 次

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

------解决方案--------------------
关注
------解决方案--------------------

------解决方案--------------------
你的需求太模糊,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>

------解决方案--------------------
通过设置display: none; 应该可以达到那样的效果.

看看:http://sunflowerbbs.oicp.net/posts/list/47.page
------解决方案--------------------
学习