js 控制DIV 预览打印

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 function doPrint()

{

    var article;

    var css;

    var strAdBegin="";

    var strAdEnd="";

    var strTmp;

    var str="<html>\n<meta http-equiv='content-type' content='text/html; charset=gb2312'>";

    css="<style>"+"td,.f12{font-size:12px}";

    css +="body{font-family:宋体;border:1px #6f6f6f solid;}";

    css +=".f24 {font-size:24px;}";

    css +=".f14 {font-size:14px;}";

    css +=".content_text{width:97%;margin-left:3%;}";

    css +=".title14 {font-size:14px;line-height:130%}";

    css +=".l17 {line-height:170%;}";

    css +=".x14 {font-size:14px;line-height:130%}";

    css +="a.zwy:link,a.zwy:active,a.zwy:visited,a.zwy:hover{text-decoration:none;color: #311bad;}";

    css +="a.htt:link,a.htt:active,a.htt:visited{text-decoration:none;color: #7b4109;}";

    css +="<\/style>";



    str +=  css;

    str +="<link href='/css/com_article.css' rel='stylesheet' type='text/css' />";

    str +=  '<meta http-equiv="content-type" content="text/html; charset=gb2312">';

    str +=  '<title>'+document.title+'<\/title>';

    str +=  "<body bgcolor=#ffffff topmargin=5 leftmargin=5 marginheight=5 marginwidth=5 >";

    str +=  "<center><table width=650  border=0 cellpadding=0 cellspacing=0 ><tr>";

    str +=  "<td align=left valign=bottom style='padding-top:12px;'>View</td>";

    str +=  "<td align=right valign=bottom style='padding-top:12px;'><a href='javascript:window.print()'><strong>Print</strong><\/a> <a href='javascript:history.back()'><strong>Back</strong><\/a><\/td><\/tr><\/table>";

    str +=  "<table  style='width:650px;border:#8EDDE6 1px solid;'><tr><td>";

    str +=document.getElementById('print_nav').innerHTML+"</td></tr><tr><td>";

    article=document.getElementById('divContext').innerHTML;

    var nv  =  document.getElementById('div_multipagenav');

    if (nv!=null)

    {    

       var nav =document.getElementById('div_multipagenav').innerHTML;

       article=article.replace(nav,"");

    }

    var hzh=document.getElementById('TomHZH');

    if (hzh !=null)

    {

      var oldhzh=document.getElementById('TomHZH').innerHTML;

      article=article.replace(oldhzh,"");

    }

    if(article.indexOf(strAdBegin)!=-1)

    {

      str +=article.substr(0,article.indexOf(strAdBegin));

      strTmp=article.substr(article.indexOf(strAdEnd)+strAdEnd.length, article.length);

    }

    else

    {

        strTmp=article

    }

    str +=strTmp

    str +=  "<\/td><\/tr><\/table><\/center><div style='height:25px;'><\/div>";

    str +=  "<\/body><\/html>";

    document.write(str);

    window.print();

    document.close();

}

    str +=document.getElementById('print_nav').innerHTML+"</td></tr><tr><td>";
    article=document.getElementById('divContext').innerHTML;  //这是获取要打印内容的地方
    var nv  =  document.getElementById('div_multipagenav');

    这三个ID必须有!DIV标记的ID

 

HTML页面内的使用方法:

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 <html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />、

<title>Print</title>

<script type="text/javascript" language="javascript" src="doPrint.js"></script>

</head>

<body>

<form id="form1">

   <div id="print_nav"></div>

   <div id="divContext">

      <img src="images/edm_01.jpg" alt="" />

   </div>

   <div id="div_multipagenav"></div>

   <input type="button" value="print" onclick="doPrint();" />

</form>

</body>

</html>

你可能感兴趣的:(div)