如何使用 dojo 重新创建包含所有子节点的现有节点?
How to recreate an existing node with all children with dojo?
我正在使用 arcgis javascript api 开发一个 webgis。
我想在用户单击 "Deactive" 按钮时销毁带有一些子节点的 "measurementID" 节点,然后使用 "Measurement" 按钮重新创建节点。
要销毁节点,我使用 dojo/dom-construct
和模块中的 destroy 方法。
但我无法重新创建节点。我如何使用 dojo 工具包重新创建节点?
registry.byId("deactivate").on("click", function() {
domConstruct.destroy("mesurementID")
});
registry.byId("Measurement").on("click", function() {
// Recreate .... !
});
<div id="mesurementID" style="position:absolute; left:50px; top:100px;width: 150px;height: 15px; z-Index:999;border:solid;border-width: 1pt;border-color: black;">
<div id="measurementDiv"></div>
</div>
有多种方法。
这里有一些可能性:
require(["dojo/dom-construct", "dojo/dom-class", "dojo/dom", "dojo/domReady!"], function(domConstruct, domClass, dom) {
//destroy the node using plain javascript:
var removedNode = dom.byId('test').removeChild(dom.byId('test1'));
//and readd it to the page
dom.byId('test').appendChild(removedNode);
//or using domConstruct
domConstruct.destroy(dom.byId('test1'));
//and recreate it
domConstruct.create('div', {
id: 'test1',
innerHTML: 'div with some content'
}, dom.byId('test'));
//or clone it
var clonedNode = dom.byId('test1').cloneNode(true);
domConstruct.destroy(dom.byId('test1'));
//and readd it
dom.byId('test').appendChild(clonedNode);
//but better, make it hidden
domClass.add(dom.byId('test1'), 'hidden');
//and show it
domClass.remove(dom.byId('test1'), 'hidden');
});
.hidden {
display: none;
}
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js"></script>
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/resources/dojo.css">
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/dojo/1.10.4/dijit/themes/tundra/tundra.css">
<div id="test">
<div id="test1">div with some content</div>
</div>
我正在使用 arcgis javascript api 开发一个 webgis。
我想在用户单击 "Deactive" 按钮时销毁带有一些子节点的 "measurementID" 节点,然后使用 "Measurement" 按钮重新创建节点。
要销毁节点,我使用 dojo/dom-construct
和模块中的 destroy 方法。
但我无法重新创建节点。我如何使用 dojo 工具包重新创建节点?
registry.byId("deactivate").on("click", function() {
domConstruct.destroy("mesurementID")
});
registry.byId("Measurement").on("click", function() {
// Recreate .... !
});
<div id="mesurementID" style="position:absolute; left:50px; top:100px;width: 150px;height: 15px; z-Index:999;border:solid;border-width: 1pt;border-color: black;">
<div id="measurementDiv"></div>
</div>
有多种方法。
这里有一些可能性:
require(["dojo/dom-construct", "dojo/dom-class", "dojo/dom", "dojo/domReady!"], function(domConstruct, domClass, dom) {
//destroy the node using plain javascript:
var removedNode = dom.byId('test').removeChild(dom.byId('test1'));
//and readd it to the page
dom.byId('test').appendChild(removedNode);
//or using domConstruct
domConstruct.destroy(dom.byId('test1'));
//and recreate it
domConstruct.create('div', {
id: 'test1',
innerHTML: 'div with some content'
}, dom.byId('test'));
//or clone it
var clonedNode = dom.byId('test1').cloneNode(true);
domConstruct.destroy(dom.byId('test1'));
//and readd it
dom.byId('test').appendChild(clonedNode);
//but better, make it hidden
domClass.add(dom.byId('test1'), 'hidden');
//and show it
domClass.remove(dom.byId('test1'), 'hidden');
});
.hidden {
display: none;
}
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js"></script>
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/resources/dojo.css">
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/dojo/1.10.4/dijit/themes/tundra/tundra.css">
<div id="test">
<div id="test1">div with some content</div>
</div>