Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/420.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript DOM操作后清空OpenLayers画布_Javascript_Dom_Html5 Canvas_Openlayers - Fatal编程技术网

Javascript DOM操作后清空OpenLayers画布

Javascript DOM操作后清空OpenLayers画布,javascript,dom,html5-canvas,openlayers,Javascript,Dom,Html5 Canvas,Openlayers,我希望用户能够快速切换到位于侧边栏中的功能。当她单击侧边栏中的特定div时,我希望该div移动到主区域,主区域中的div移动到侧边栏(交换内容)。因此,我实现了一个javascript函数来交换css类和这些div的DOM位置 var node1 = document.querySelector(".one"); var node2 = document.querySelector(".other"); node1.classList.toggle("other"); node1.classLi

我希望用户能够快速切换到位于侧边栏中的功能。当她单击侧边栏中的特定div时,我希望该div移动到主区域,主区域中的div移动到侧边栏(交换内容)。因此,我实现了一个javascript函数来交换css类和这些div的DOM位置

var node1 = document.querySelector(".one");
var node2 = document.querySelector(".other");
node1.classList.toggle("other");
node1.classList.toggle("one");
node2.classList.toggle("one");
node2.classList.toggle("other");
var clone1 = node1.cloneNode(true);
var clone2 = node2.cloneNode(true);
node2.parentNode.replaceChild(clone1, node2);
node1.parentNode.replaceChild(clone2, node1);
可以很好地工作,例如在div中使用图片。如果我把一个简单的OpenLayers贴图放在其中一个div中,它会显示它应该显示的内容,直到我单击并交换贴图(或者放到主区域或者边栏中)。在一个空的画布上,我只得到控件和属性按钮(至少后者不起作用)

虽然我可能需要的只是一个映射刷新(我尝试了Stackoverflow上提供的一些方法,如刷新、重画、渲染、更新大小等),但我也尝试了再次初始化映射(就像我最初做的那样),但没有用

<div class="sidebar" onclick="Util.swapNodes(event); Map.initMap();">


知道这里发生了什么吗?

好的,修好了。关键是要做两件事:

首先,我必须从OperLayers生成的div中清除DOM,该div保存地图及其按钮(可能是在不同DOM位置对“旧”地图的引用);它的类是
ol viewport

其次,在交换节点(如上面的代码所示)之后,我还必须更改map节点的ID;至少我没有找到直接“刷新”旧ID的方法

所以,我所做的是:

var mapNode = document.querySelector(".map"); // finds the class
if (mapNode.parentNode.classList.contains("one")) {
    mapNode.id = "mapone"; // change the id
    document.querySelector(".ol-viewport").remove(); // remove old div
    map.setTarget("mapone"); // re-set target to new id
} else if (mapNode.parentNode.classList.contains("other")) {
    mapNode.id = ("mapother"); // change the id
    document.querySelector(".ol-viewport").remove(); // remove old div
    map.setTarget("mapother");  // re-set target to new id
}
var mapNode = document.querySelector(".map"); // finds the class
if (mapNode.parentNode.classList.contains("one")) {
    mapNode.id = "mapone"; // change the id
    document.querySelector(".ol-viewport").remove(); // remove old div
    map.setTarget("mapone"); // re-set target to new id
} else if (mapNode.parentNode.classList.contains("other")) {
    mapNode.id = ("mapother"); // change the id
    document.querySelector(".ol-viewport").remove(); // remove old div
    map.setTarget("mapother");  // re-set target to new id
}