使用 JQuery 将内容写入新窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3841100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Write Content To New Window with JQuery
提问by user462166
I have a web page with a DIV element in it. When a user clicks "print", I want to print the contents of that div. Please note, I only want to print the contents of the DIV, not the entire page. To attempt this, I decided I would open a new window using JavaScript. I was then going to write the contents of the DIV into the new window. My question is, is this possible with JQuery? If so, how? Currently, I'm trying the following:
我有一个带有 DIV 元素的网页。当用户单击“打印”时,我想打印该 div 的内容。请注意,我只想打印 DIV 的内容,而不是整个页面。为了尝试这个,我决定使用 JavaScript 打开一个新窗口。然后我打算将 DIV 的内容写入新窗口。我的问题是,这可以用 JQuery 实现吗?如果是这样,如何?目前,我正在尝试以下操作:
function printClick() {
var w = window.open();
var html = $("#divToPrintID").html();
// how do I write the html to the new window with JQuery?
}
回答by Steve Greatrex
回答by Alpesh
Try -
尝试 -
var w = window.open();
var html = $("#divToPrintID").html();
w.document.writeln(html);
Reference - http://www.javascripter.net/faq/writingt.htm
回答by ITroubs
file1:
文件 1:
function find(input){
return $(input);
}
function printClick() {
var w = window.open();
var html = $("#divToPrintID").html();
// how do I write the html to the new window with JQuery?
}
and in the second file do that:
在第二个文件中这样做:
var html = window.opener.find("#divToPrintID").html();
回答by FatherStorm
I found a pretty perfect match of a tutorial for exactly that.. http://www.bennadel.com/blog/1591-Ask-Ben-Print-Part-Of-A-Web-Page-With-jQuery.htm
我找到了一个非常完美的教程匹配.. http://www.bennadel.com/blog/1591-Ask-Ben-Print-Part-Of-A-Web-Page-With-jQuery.htm
Additionally, you can attact print-only CSS to the page that has everything but what you want turned off.
此外,您可以将仅打印的 CSS 附加到页面中,该页面除了您想要关闭的内容外,还包含所有内容。