Javascript jQuery print()div未呈现
我正在使用jQuery print()打印div打包列表。它确实可以工作,但第一次单击“打印”按钮时,div为空,第二次单击时,div呈现为空。我能知道为什么会这样吗?另外,当我使用谷歌浏览器时,它在打印之前就已经打印出来了。。。。但当我使用firefox或IE时,并没有打印预览功能……在打印之前如何启用打印预览功能?A. js jquery.print.jsJavascript jQuery print()div未呈现,javascript,jquery,css,Javascript,Jquery,Css,我正在使用jQuery print()打印div打包列表。它确实可以工作,但第一次单击“打印”按钮时,div为空,第二次单击时,div呈现为空。我能知道为什么会这样吗?另外,当我使用谷歌浏览器时,它在打印之前就已经打印出来了。。。。但当我使用firefox或IE时,并没有打印预览功能……在打印之前如何启用打印预览功能?A. js jquery.print.js // Create a jquery plugin that prints the given element. jQuery.fn.p
// Create a jquery plugin that prints the given element.
jQuery.fn.print = function() {
// NOTE: We are trimming the jQuery collection down to the
// first element in the collection.
if (this.size() > 1) {
this.eq(0).print();
return;
} else if (!this.size()) {
return;
}
// ASSERT: At this point, we know that the current jQuery
// collection (as defined by THIS), contains only one
// printable element.
// Create a random name for the print frame.
var strFrameName = ("printer-" + (new Date()).getTime());
// Create an iFrame with the new name.
var jFrame = $("<iframe name='" + strFrameName + "'>");
// Hide the frame (sort of) and attach to the body.
jFrame
.css("width", "1px")
.css("height", "1px")
.css("position", "absolute")
.css("left", "-9999px")
.appendTo($("body:first"))
;
// Get a FRAMES reference to the new frame.
var objFrame = window.frames[strFrameName];
// Get a reference to the DOM in the new frame.
var objDoc = objFrame.document;
// Grab all the style tags and copy to the new
// document so that we capture look and feel of
// the current document.
// Create a temp document DIV to hold the style tags.
// This is the only way I could find to get the style
// tags into IE.
var jStyleDiv = $("<div>").append(
$("style").clone()
);
// Write the HTML for the document. In this, we will
// write out the HTML of the current element.
objDoc.open();
objDoc.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
objDoc.write("<html>");
objDoc.write("<body>");
objDoc.write("<head>");
objDoc.write("<title>");
objDoc.write(document.title);
objDoc.write("</title>");
objDoc.write(jStyleDiv.html());
objDoc.write( "<link rel=\"stylesheet\" href=\"http://localhost:81/travelwithus/css/main.css\" type=\"text/css\" media=\"print\" />" );
objDoc.write("</head>");
objDoc.write(this.html());
objDoc.write("</body>");
objDoc.write("</html>");
objDoc.close();
// Print the document.
objFrame.focus();
objFrame.print();
// Have the frame remove itself in about a minute so that
// we don't build up too many of these frames.
setTimeout(
function() {
jFrame.remove();
},
(60 * 1000)
);
}
//创建打印给定元素的jquery插件。
jQuery.fn.print=函数(){
//注意:我们正在将jQuery集合缩减到
//集合中的第一个元素。
if(this.size()>1){
这个.eq(0).print();
返回;
}否则如果(!this.size()){
返回;
}
//断言:此时,我们知道当前jQuery
//集合(如本文所定义)仅包含一个
//可打印元素。
//为打印框创建随机名称。
var strFrameName=(“打印机-”+(新日期()).getTime());
//使用新名称创建iFrame。
var jFrame=$(“”);
//隐藏框架(某种程度上)并附加到主体。
jFrame
.css(“宽度”、“1px”)
.css(“高度”、“1px”)
.css(“位置”、“绝对”)
.css(“左”,“-9999px”)
.appendTo($(“正文:第一个”))
;
//获取新帧的帧引用。
var objFrame=window.frames[strFrameName];
//获取对新框架中DOM的引用。
var objDoc=objFrame.document;
//抓取所有样式标记并复制到新的
//文档,以便我们捕获
//当前文档。
//创建临时文档DIV以保存样式标记。
//这是我能找到的唯一得到这种风格的方法
//将标签插入IE。
var jStyleDiv=$(“”)。追加(
$(“样式”).clone()
);
//为文档编写HTML。在这里,我们将
//写出当前元素的HTML。
objDoc.open();
objDoc.write(“”);
objDoc.write(“”);
objDoc.write(“”);
objDoc.write(“”);
objDoc.write(“”);
objDoc.write(文件名);
objDoc.write(“”);
write(jStyleDiv.html());
objDoc.write(“”);
objDoc.write(“”);
write(this.html());
objDoc.write(“”);
objDoc.write(“”);
objDoc.close();
//打印文档。
objFrame.focus();
objFrame.print();
//在大约一分钟内将框架自行拆除,以便
//我们不会建立太多这样的框架。
设置超时(
函数(){
jFrame.remove();
},
(60 * 1000)
);
}
你看过这篇文章了吗:我想这是因为IE没有预览功能。我能做些什么吗?
// Create a jquery plugin that prints the given element.
jQuery.fn.print = function() {
// NOTE: We are trimming the jQuery collection down to the
// first element in the collection.
if (this.size() > 1) {
this.eq(0).print();
return;
} else if (!this.size()) {
return;
}
// ASSERT: At this point, we know that the current jQuery
// collection (as defined by THIS), contains only one
// printable element.
// Create a random name for the print frame.
var strFrameName = ("printer-" + (new Date()).getTime());
// Create an iFrame with the new name.
var jFrame = $("<iframe name='" + strFrameName + "'>");
// Hide the frame (sort of) and attach to the body.
jFrame
.css("width", "1px")
.css("height", "1px")
.css("position", "absolute")
.css("left", "-9999px")
.appendTo($("body:first"))
;
// Get a FRAMES reference to the new frame.
var objFrame = window.frames[strFrameName];
// Get a reference to the DOM in the new frame.
var objDoc = objFrame.document;
// Grab all the style tags and copy to the new
// document so that we capture look and feel of
// the current document.
// Create a temp document DIV to hold the style tags.
// This is the only way I could find to get the style
// tags into IE.
var jStyleDiv = $("<div>").append(
$("style").clone()
);
// Write the HTML for the document. In this, we will
// write out the HTML of the current element.
objDoc.open();
objDoc.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
objDoc.write("<html>");
objDoc.write("<body>");
objDoc.write("<head>");
objDoc.write("<title>");
objDoc.write(document.title);
objDoc.write("</title>");
objDoc.write(jStyleDiv.html());
objDoc.write( "<link rel=\"stylesheet\" href=\"http://localhost:81/travelwithus/css/main.css\" type=\"text/css\" media=\"print\" />" );
objDoc.write("</head>");
objDoc.write(this.html());
objDoc.write("</body>");
objDoc.write("</html>");
objDoc.close();
// Print the document.
objFrame.focus();
objFrame.print();
// Have the frame remove itself in about a minute so that
// we don't build up too many of these frames.
setTimeout(
function() {
jFrame.remove();
},
(60 * 1000)
);
}