2012-03-19 80 views
1

我希望通过传递custome html代码来打印文档。通过javascript打印html/jquery

非工作代码,我已经写了:

function Clickheretoprint() 
{ 
    var disp_setting="toolbar=yes,location=no,directories=yes,menubar=yes,"; 
     disp_setting+="scrollbars=yes,width=780, height=780, left=100, top=25"; 
    var content_vlue = document.getElementById("result_tbl").innerHTML; 

    var docprint=window.open("","",disp_setting); 
    docprint.document.open(); 
    docprint.document.write('<html><head><title>Ashley Mattresses</title>'); 
    docprint.document.write('</head><body onLoad="self.print()"><center>');   
    docprint.document.write('<TABLE width="100%" cellpadding=10 align=center valign="top"><TR valign="top"><TD width = "33%" valign="top">col1</TD><TD width = "33%" valign="top">col2</TD><TD width = "33%" valign="top">col3</TD></TR></TABLE>');  
    docprint.document.write('</center></body></html>'); 
    docprint.document.close(); 
    docprint.focus(); 
    docprint.close(); 
} 

这是我叫一个锚标记的HREF的方法,但没有得到所做的工作:

<a href="javascript:Clickheretoprint()"> 

我作为JavaScript/jQuery编码的新宠儿,请帮助我实现这一目标(通过纠正这一问题或提出一种方法),作为其迫切要求。

在此先感谢。

+2

OH NO迫切?什么不起作用?控制台中是否有错误?你知道可以在没有新窗口的情况下只在页面上打印某些东西。看看CSS打印介质。 – epascarello 2012-03-19 12:22:12

+0

@epascarello ya我重新检查了一点修改,发现在Firefox中工作,而不是在谷歌浏览器。早些时候我正在测试Chrome本身,所以无法打印。但也需要使用Chrome浏览器,请问您可以提出一些建议吗? 此外,感谢您对CSS打印介质的建议,它非常好,并且肯定会要求此功能。 – HarsH1610 2012-03-19 13:33:22

+0

答案#2是正确的,但您必须添加:var disp_setting =“menubar = no; status = no; toolbar = no;”;或您选择的设置 – 2013-02-12 21:20:55

回答

7

您正处于正确的轨道上。假设result_tbl是你想打印原样,先用<div>标签例如为:

<div> 
    <table id="result_tbl"> 
     ..... 
    </table> 
</div> 

包裹元件然后让这样的代码元素的ID:

var docprint=window.open("about:blank", "_blank", disp_setting); 
var oTable = document.getElementById("result_tbl"); 
docprint.document.open(); 
docprint.document.write('<html><head><title>Ashley Mattresses</title>'); 
docprint.document.write('</head><body><center>'); 
docprint.document.write(oTable.parentNode.innerHTML); 
docprint.document.write('</center></body></html>'); 
docprint.document.close(); 
docprint.print(); 
docprint.close(); 

Live test case

Chrome的18岁以下的罚款的工作对我来说:

print is working

+0

感谢您的努力,检查了现场测试案例,您已经提供了上面的,它的工作原理和通过mozilla-firefox打印很好,但无法用铬做同样的事情,请你重新检查,并建议我可能的出路? – HarsH1610 2012-03-19 12:59:24

+0

@ HarsH1610 Chrome 18在这里,点击按钮打开浏览器的打印页面就好了。 – 2012-03-19 14:30:52

+0

你能否检查一下:在chrome的打印页面中,打印按钮被禁用,并且没有任何页面的打印预览;你的结局是否一样?请建议! – HarsH1610 2012-03-19 15:03:55

1

结帐:http://jsfiddle.net/5Wfqr/3/

对我来说只是麻烦的来源是:你的HTML中的 “result_tbl”:

var content_vlue = document.getElementById("result_tbl").innerHTML; 

你有ID的元素?

+0

是的,我确实拥有它,只是忘了从这个示例代码中删除它的引用,但它不起作用,虽然我删除了它。 – HarsH1610 2012-03-19 12:53:15

+0

感谢您使用firefox而不是使用google-chrome浏览器的努力,您能否提出建议? – HarsH1610 2012-03-19 13:31:58

0

此代码工作正常,我

<body> 
     <script type="text/javascript"> 
      function printPage() { 

       var docprint = window.open("about:blank", "_blank"`enter code here`, "channelmode");  
       var oTable = document.getElementById("tbl"); 
       docprint.document.open(); 
       docprint.document.write('<html><head><title>your title</title>'); 
       docprint.document.write('</head><body><center>'); 
       docprint.document.write(oTable.innerHTML); 
       docprint.document.write('</center></body></html>'); 
       docprint.document.close(); 
       docprint.print(); 
       docprint.close(); 
      } 
     </script> 
     <table id="tbl"> 
      <tr> 
       <td> content1 </td> 
       <td> content2 </td> 
      </tr> 
     </table> 
     <input type="button" value ="print" id="printButton" onclick="javascript:printPage()"/> 
    </body>