How to create “svg” object without appending it?

后端 未结 6 571
天涯浪人
天涯浪人 2020-12-16 09:24

Consider the following code:

var svg = d3.select(\'#somediv\').append(\"svg\").attr(\"width\", w).attr(\"height\", h);

I would like to refa

6条回答
  •  悲哀的现实
    2020-12-16 10:01

    You can use the following:

    var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
    

    Note the use of createElementNS. This is required because svg elements are not in the same XHTML namespace as most HTML elements.

    This code creates a new svg element, as you would regardless of using D3 or not, and then creates a selection over that single element.

    This can be made marginally more succinct but clearer and less error prone as:

    var svg = document.createElementNS(d3.ns.prefix.svg, 'svg');
    

提交回复
热议问题