如何通过使用javascript删除在内存中创建的元素

问题描述 投票:0回答:1

这是我的情况


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);
}

我尝试了一些方法,但仍然无法正常工作。

enter image description here

参考

https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/remove

javascript dom
1个回答
0
投票

将要创建的id设置为Div,然后使用id将其删除

const vdom = document.createElement(`div`);
vdom.setAttribute("id", "Div1");
document.getElementById("Div1").remove();
© www.soinside.com 2019 - 2024. All rights reserved.