使用JQuery将内容写入新窗口

use*_*166 40 jquery

我有一个带有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?
}
Run Code Online (Sandbox Code Playgroud)

Ste*_*rex 52

您可以使用

$(w.document.body).html(html);
Run Code Online (Sandbox Code Playgroud)

这个例子


Alp*_*esh 5

试试 -

var w = window.open();
var html = $("#divToPrintID").html();
w.document.writeln(html);
Run Code Online (Sandbox Code Playgroud)

参考 - http://www.javascripter.net/faq/writingt.htm


ITr*_*ubs 4

文件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?
}
Run Code Online (Sandbox Code Playgroud)

并在第二个文件中执行以下操作:

var html = window.opener.find("#divToPrintID").html();
Run Code Online (Sandbox Code Playgroud)