L@_@K更多内容参考 JScript语言参考.rar
http://download.csdn.net/source/308916<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title> new document </title>
  <meta name="generator" content="editplus" />
  <meta name="author" content="" />
  <meta name="keywords" content="" />
  <meta name="description" content="" />
 </head> <body>
<input type="text" id="" value="Tom" />
<!--startprint-->
123 我的票据。
321
over.
<!--endprint-->
<input type="button" value="print" onclick="preview()" />
  <script type="text/javascript">
  <!--
function preview()
{
var bdhtml=window.document.body.innerHTML;
var sprnstr="<!--startprint-->";
var eprnstr="<!--endprint-->";
/*
substr 方法
返回一个从指定位置开始的指定长度的子字符串。
stringvar.substr(start [, length ])
 */
var start = bdhtml.indexOf(sprnstr) + sprnstr.length;
var prnhtml=bdhtml.substr(start);
/*
substring 方法
返回位于 String 对象中指定位置的子字符串。 
strVariable.substring(start, end)
 */
prnhtml=prnhtml.substring(0, prnhtml.indexOf(eprnstr));
window.document.body.innerHTML=prnhtml;
window.print();
window.document.body.innerHTML=bdhtml;
}
  //-->
  </script>
 </body>
</html>

解决方案 »

  1.   

    告诉你另一种解决方案,通过 style 标签的 media 属性控制打印显隐!
    L@_@k
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
     <head>
      <title>css.style.media.printControl.html</title>
      <meta name="generator" content="editplus" />
      <meta name="author" content="[email protected]" />
      <meta name="keywords" content="css style media print" />
      <meta name="description" content="" />
      <style type="text/css" media="screen">
    .noPrint {
    display: block;
    }
      </style>
      <style type="text/css" media="print">
    .noPrint {
    display: none;
    }
      </style>
      <style type="text/css">
    input {
    width: 50px;
    background-color: green;
    color: white;
    }
    table {
    border-collapse: collapse;
    }
    td {
    border-style: solid;
    border-width: 1px;
    border-color: red;
    }
      </style> </head> <body>
      <div class="noPrint">
    <h3>通过 style 标签的 media 属性控制打印显隐。</h3>
    <input type="text" id="" value="OK" />
      </div>
      <div>
    票据表格:
    <table>
    <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    </tr>
    <tr>
    <td>a</td>
    <td>b</td>
    <td>c</td>
    </tr>
    <tr>
    <td>I</td>
    <td>II</td>
    <td>III</td>
    </tr>
    </table>
      </div>
      <div class="noPrint">
    <input type="text" id="" value="KO" />
      </div>
     </body>
    </html>