How can I select :last-child in d3.js?

后端 未结 6 2156
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-31 08:55

I need to manipulate the text elements of the first and last tick of an axis to bring them more towards the center.

I am trying to select them, one at t

6条回答
  •  猫巷女王i
    2020-12-31 09:37

    They don't exist in d3 specifically, but you can use the .firstChild and .lastChild methods on a node.

    You can first select all of the parents of the node, and then operate within the scope of a .each() method, like so:

    d3.selectAll('.myParentElements').each(function(d,i){
      var firstChild = this.firstChild,
          lastChild = this.lastChild;
    
      //Do stuff with first and last child
    });
    

    Within the scope of .each(), this refers to the individual node, which is not wrapped by a d3 selection, so all of the standard methods on a node are available.

提交回复
热议问题