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

前端 未结 9 1069
误落风尘
误落风尘 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:46

    If you're using jQuery, you can just do

    $("#svgid").empty();
    

    This deletes all child elements of the svg while leaving its other attributes like width and height intact.

    0 讨论(0)
  • 2020-12-29 01:46

    use this? http://keith-wood.name/svg.html

    there's also raphael: jQuery SVG vs. Raphael

    I'd be tempted to trawl through and see who they're doing their .destroy() methods.

    0 讨论(0)
  • 2020-12-29 01:46

    If you want to keep defs of your svg as me use this

    function clear(prnt){
        let children = prnt.children;
        for (let i=0;i<children.length;){
            let el = children[i];
            if (el.tagName!=='defs'){
                el.remove();
            }else(i++);
        }
    }

    0 讨论(0)
提交回复
热议问题