outerHTML of an SVG element

前端 未结 5 1994
天命终不由人
天命终不由人 2020-12-09 03:46

Why can not we get outerHTML of an svg element with element.outerHTML property?

Is this way is the best http://jsfiddle.net/33g8g/ for getting svg sourc

5条回答
  •  无人及你
    2020-12-09 04:03

    SVGElements don't have outerHTML property.

    You can define like this in pure Javascript

    Object.defineProperty(SVGElement.prototype, 'outerHTML', {
        get: function () {
            var $node, $temp;
            $temp = document.createElement('div');
            $node = this.cloneNode(true);
            $temp.appendChild($node);
            return $temp.innerHTML;
        },
        enumerable: false,
        configurable: true
    });
    

    Or a one line jQuery solution would be

    $('
    ').append($(svgElement).clone()).html();

    Reference: https://gist.github.com/jarek-foksa/2648095

提交回复
热议问题