zoukankan      html  css  js  c++  java
  • 总结JS打印方法

    原文地址:http://blog.sina.com.cn/s/blog_77ed775e01011ek7.html

    一.用JS自带函数打印

    <input id="btnPrint" type="button" value="打印" onclick="javascript:window.print();" />

    <input id="btnPrint" type="button" value="打印预览" onclick=preview(1) />
    <style type="text/css" media=print>
    .noprint{display : none }
    </style>


    <p class="noprint">不需要打印的地方</p>

    <script>
    function preview(oper)       
    {
    if (oper < 10)
    {
    bdhtml=window.document.body.innerHTML;//获取当前页的html代码
    sprnstr="<!--startprint"+oper+"-->";//设置打印开始区域
    eprnstr="<!--endprint"+oper+"-->";//设置打印结束区域
    prnhtml=bdhtml.substring(bdhtml.indexOf(sprnstr)+18); //从开始代码向后取html

    prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));//从结束代码向前取html
    window.document.body.innerHTML=prnhtml;
    window.print();
    window.document.body.innerHTML=bdhtml;
    } else {
    window.print();
    }
    }
    </script>
    <p>XXXXX</p>
    <!--startprint1-->要打印的内容<!--endprint1-->

    再加个打印按纽 onclick=preview(1)


    2、调用windows底层打印,报安全警告,不建议使用(不支持局部打印)
    <HTML>
    <HEAD>
    <TITLE>javascript打印-打印页面设置-打印预览代码</TITLE>
    <META http-equiv=Content-Type content="text/html; charset=gb2312" />
    <SCRIPT language=javascript> 
      function printsetup(){ 
      // 打印页面设置 
      wb.execwb(8,1); 
      } 
      function printpreview(){ 
      // 打印页面预览 
         
      wb.execwb(7,1);        
         
      } 

      function printit() 
      { 
      if (confirm('确定打印吗?')) { 
      wb.execwb(6,6);
      } 
      } 
      </SCRIPT>
    </HEAD>
    <BODY>

    <DIV align=center>
    <OBJECT id=wb height=0 width=0 
    classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 name=wb></OBJECT>
    <INPUT onclick=javascript:printit() type=button value=打印 name=button_print /> 
    <INPUT onclick=javascript:printsetup(); type=button value=打印页面设置 name=button_setup /> 
    <INPUT onclick=javascript:printpreview(); type=button value=打印预览 name=button_show /> 
    一按开始的减肥了卡时间段
    </DIV>
    </BODY>
    </HTML>

    3jQuery实现(支持局部打印)
    <script type="text/javascript" src="jquery-1.4.2.min.js"></script>
    <script type="text/javascript" src="jquery.PrintArea.js"></script>
    <script>
    $(document).ready(function(){
      $("input#biuuu_button").click(function(){

      $("div#myPrintArea").printArea();

    });
    });
     
    </script>

    <input id="biuuu_button" type="button" value="打印"></input>

    <div id="myPrintArea">.....文本打印部分.....</div>

    Query插件PrintArea完整方法如下:

      1. (function($) {
      2. var printAreaCount = 0;
      3. $.fn.printArea = function()
      4. {
      5. var ele = $(this);
      6. var idPrefix = "printArea_";
      7. removePrintArea( idPrefix + printAreaCount );
      8. printAreaCount++;
      9. var iframeId = idPrefix + printAreaCount;
      10. var iframeStyle = 'position:absolute;0px;height:0px;left:-500px;top:-500px;';
      11. iframe = document_createElement_x('IFRAME');
      12. $(iframe).attr({ style : iframeStyle,
      13. id    : iframeId
      14. });
      15. document.body.a(iframe);
      16. var doc = iframe.contentWindow.document;
      17. $(document).find("link")
      18. .filter(function(){
      19. return $(this).attr("rel").toLowerCase() == "stylesheet";
      20. })
      21. .each(function(){
      22. doc.write('<link type="text/css" rel="stylesheet" href="' +
      23. $(this).attr("href") + '" >');
      24. });
      25. doc.write('<div class="' + $(ele).attr("class") + '">' + $(ele).html() + '</div>');
      26. doc.close();
      27. var frameWindow = iframe.contentWindow;
      28. frameWindow.close();
      29. frameWindow.focus();
      30. frameWindow.print();
      31. }
      32. var removePrintArea = function(id)
      33. {
      34. $( "iframe#" + id ).remove();
      35. };
      36. })(jQuery);
  • 相关阅读:
    【转】Python爬虫(5)_性能相关
    【转】Python爬虫(4)_selenium模块
    【转】Python爬虫(3)_Beautifulsoup模块
    【转】Python爬虫_示例2
    【转】Python爬虫_示例
    linux开机自启动tomcat
    Linux下Tomcat启动,停止命令
    Linux下tomcat启动Neither the JAVA_HOME nor the JRE_HOME environment variable is defined At least one of
    centos7开启关闭80端口
    centos安装JDK
  • 原文地址:https://www.cnblogs.com/yony/p/2718008.html
Copyright © 2011-2022 走看看