Is there an easy way to clear an SVG element's contents?

前端 未结 9 1173
误落风尘
误落风尘 2020-12-29 01:14

In HTML, I can clear a

element with this command:

div.innerHTML = \"\";

Is there an equivalent if I have an

9条回答
  •  無奈伤痛
    2020-12-29 01:43

    You can use the clone and replace the element with the cloned one.

        var parentElement = svg.parentElement
        var emptySvg = svg.cloneNode(false);
        parentElement.removeChild(svg);
        parentElement.appendChild(emptySvg);
    

    This will append the svg at the end, you might want to get the element before and append accordinaly

提交回复
热议问题