4

我正在尝试将 DOM 节点从“根”页面移动到通过window.open(). 这是我正在使用的代码。

var win = window.open('/Search/Print', 'printSearchResults'),
    table = $('#printTable');
win.document.close();
setTimeout(function () {
    var el = win.document.createElement("table");
    el.innerHTML = table.html();
    win.document.body.appendChild(el);
}, 40);

它在 Chrome 中工作,但在 IE8 中,我收到以下错误:“未知的运行时错误。”

我也试过这样:

var p = window.open('/Search/Print', 'printSearchResults'),
    table = $('#printTable');
setTimeout(function () {
    p.document.body.appendChild(table.clone(false)[0]);
}, 100);

这样做,我在 IE8 中收到“不支持此类接口”。同样,Chrome 工作正常。

有没有人有办法做我想要实现的目标?

为了完整起见,这里是弹出窗口的 HTML:

<!DOCTYPE html>
  <html>
  <head>
      <title>Print Results</title>
  </head>
  <body>
  </body>
</html>
4

2 回答 2

1

为了能够使用 iframe 和新窗口,您应该在 write() 之前使用 addres: about:blank 对其进行初始化。另请注意,加载/打开窗口/框架需要时间,因此您不能立即给它们写信。设置超时,或检查 onload。请参阅此答案以获取更多信息。

祝你好运!

于 2012-08-08T16:25:06.903 回答
1

我在 IE9(和 IE8/7 浏览器模式)上测试了您的代码。

代替el.innerHTML = table.html();

使用 jquery$(el).html(table.html());解决了这个问题。

于 2012-08-09T13:03:53.277 回答