Can I move an SVG element between SVG groups, in d3.js

后端 未结 4 2063
心在旅途
心在旅途 2020-12-10 14:24

Can I move an SVG element between SVG groups - without stirring too much calculation behind the scenes nor crafting too much code in my own code? The d3 api documentation me

4条回答
  •  情话喂你
    2020-12-10 15:10

    The docs you posted also mention that you can re-add the elements by passing a function to .append or .insert. Here's what it says:

    Note that there is not currently a dedicated API to add removed elements back to the document; however, you can pass a function to selection.append or selection.insert to re-add elements.


    Since using .remove on a selection returns that selection, you could store the removed element in a variable, and then .append it to the new group using selection.node() to grab the DOM element from the removed selection:

    var removed = yourSelection.remove();
    yourTargetGroup.append(function() {
      return removed.node();
    });
    

    Here's a simple Fiddle that uses this technique to move a circle element from one group to another in a click handler.

提交回复
热议问题