通过javascript / jquery打印html [英] print html through javascript / jquery

查看:70
本文介绍了通过javascript / jquery打印html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我写的非工作代码:

 函数Clickheretoprint()
{
var disp_setting =toolbar = yes,location = no,directories = yes,menubar =是,;
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>< /表>');
docprint.document.write('< / center>< / body>< / html>');
docprint.document.close();
docprint.focus();
docprint.close();
}

这是我在锚标记的href上调用的方法,但不是完成工作:

 < a href =javascript:Clickheretoprint()> 

我是一个新的用于javascript / jQuery编码的蜜蜂,请帮我实现相同的纠正这种情况或提出一种方法)作为一项紧急要求。

提前致谢

解决方案

你走在正确的轨道上。假设 result_tbl 是您想按原样打印的元素的ID,请首先用< div> 标记例如:

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

然后有这样的代码:

  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();

现场测试案例



在Chrome 18下正常工作: b
$ b


I wish to print a document by passing custome html code.

the non-working code i have written :

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();
}

This is the method I called on href of an anchor tag, but the not getting the work done:

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

I am a new-bee to javascript/jQuery coding, please help me to achieve the same (by correcting this or suggesting an approach) as its an urgent requirement.

Thanks in advance.

解决方案

You are on the right track. Assuming result_tbl is the ID of element you like to print as-is, first wrap the element with a <div> tag e.g.:

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

Then have such code:

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.

Working fine for me under Chrome 18:

这篇关于通过javascript / jquery打印html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆