这是我的情况
const virtualDomConvert = (filename = ``) => {
const svg = document.querySelector(`[id="live_map_svg"]`);
const clone = svg.cloneNode(true);
clone.id = 'vdom_svg';
// autoRemoveAttributes(clone);
const html = clone.outerHTML;
// add xml namespace, support browser open preview
const xml = `
<?xml version="1.0" encoding="UTF-8"?>
${html}
`.trim();
const alink = document.createElement('a');
alink.setAttribute('href', 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(xml));
alink.setAttribute('download', filename);
alink.style.display = 'none';
const vdom = document.createElement(`div`);
vdom.appendChild(alink);
alink.click();
vdom.removeChild(alink);
// ❓ how to delete vdom ???
// document.body.appendChild(alink);
// alink.click();
// document.body.removeChild(alink);
}
https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/remove
将要创建的id
设置为Div
,然后使用id
将其删除
const vdom = document.createElement(`div`);
vdom.setAttribute("id", "Div1");
document.getElementById("Div1").remove();