js如何实现打印指定区域的内容

来源:互联网 发布:密立根油滴实验数据 编辑:程序博客网 时间:2024/05/21 15:41

方法一:

 <script language="javascript" type="text/javascript">

    function printpage(myDiv){ 
   
    //var newstr = document.all.item(myDiv).innerHTML; 
    var newstr = document.getElementById(myDiv).innerHTML;
     alert(newstr);
    var oldstr = document.body.innerHTML; 
    document.body.innerHTML = newstr; 
    window.print(); 
    document.body.innerHTML = oldstr; 
    return false; 
    } 
    </script>

 

   <div id="myDiv"  >说明:打印整个div区域的内容,如果包含按钮,则按钮也会打印出来!
    <div>
      打印内容打印内容打印内容打印内容打印内容打印内容打印内容打印内容
    adfhioasdhfiohasdofihosdhfosdhfiosdhfiosdfhsdfhsdifsidfiosdfhosdhf
        </div>
    <input type="button" id="bt" onclick="javascript:printpage('myDiv')"   value="打印" />
    </div>

 

 

方法二:

<script language="javascript" type="text/javascript">
        function doPrint() { 
        bdhtml=window.document.body.innerHTML; 
        sprnstr="<!--startprint-->"; 
        eprnstr="<!--endprint-->"; 
        prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr)+17); 
        prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr)); 
        window.document.body.innerHTML=prnhtml; 
        window.print(); 
        } 
        </script>

 

<div>
    <!--startprint-->说明:这是开始打印的位置
    打印内容打印内容打印内容打印内容打印内容打印内容打印内容打印内容
    adfhioasdhfiohasdofihosdhfosdhfiosdhfiosdfhsdfhsdifsidfiosdfhosdhf
    <!--endprint-->说明:这是结束打印的位置
    </div>
    <input type="button" id="bt" onclick="javascript:doPrint()"   value="打印" />
    </div>

 

用户在打印网页的时候,页面上会出现网页的名字,页码,链接地址和打印时间,如果用户不需要这些信息,则需要设置用户的网页设置:

具体如下:文件-》页面设置-》将页眉页脚中的代码删除即可。打印出来的文档没有页码信息和链接地址信息

原创粉丝点击