1

我正在尝试创建一个按钮,该按钮将打印嵌入在网页上的谷歌地图

见代码:

function print(){


var contents = window.opener.document.getElementById("map_canvas"); 
document.write(contents.innerHTML); 
window.print(); 

}

这是保存我的地图的 div

<div id="map_canvas" style="width:800px; height:500px;"></div>  

这是打印按钮

<input type="button" value="Print" onclick="print()">

当我单击打印按钮时,出现错误“window.opener 为空”。打印地图的正确代码是什么?

4

3 回答 3

0

window.opener 返回对创建窗口的窗口的引用。由于您没有创建任何新窗口,因此此处不需要 window.opener。

直接使用 window.document.getElementById。

尝试这个:

function print(){
    var contents = window.document.getElementById("map_canvas"); 
    document.write(contents.innerHTML); 
    window.print(); 
    }
于 2013-04-25T12:59:10.533 回答
0
var content = window.document.getElementById("map-canvas"); // get you map details
var newWindow = window.open(); // open a new window
newWindow.document.write(content.innerHTML); // write the map into the new window
newWindow.print(); // print the new window
于 2015-04-24T13:39:00.477 回答
-1

检查主窗口上包含地图的 DIV 是否具有 id="map_canvas" 的 div

于 2011-11-03T16:30:46.060 回答