d3相当于jQuery parent()

前端之家收集整理的这篇文章主要介绍了d3相当于jQuery parent()前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有各种SVG< g>对象,每个对象都有一个< circle>孩子和< text>儿童.我可以使用select()来查找特定的< text>对象,由附加到它的类,然后修改它:
  1. d3.select('text.my-class')
  2. .classed("my-class",false).classed("new-class",true)
  3. .text("Next Stage!")
  4. ;

现在我需要修改它的圆形兄弟.圆圈没有特定的识别类(嗯……也许给它一个d3的方式来做这个?),所以我的第一次尝试就像jQuery一样:

  1. d3.select('text.my-class').parent().select('circle')
  2. .attr('style','fill:#f00;')
  3. ;

这失败了“父母不是一个功能”.

类似问题(How to select parent element of current element in d3.js)的答案提示this.parentNode,但要么我使用它错了要么在这里不起作用.我试过这两个:

  1. d3.select('text.my-class').select(parentNode).select('circle')
  2. d3.select('text.my-class').select(this.parentNode).select('circle')

解决方法

D3没有访问父节点的方法.您可以使用node()方法访问所选元素的DOM节点.该元素将具有parentNode属性
  1. var textNode = d3.select('text.my-class').node(),// DOM node
  2. parentNode = textNode.parentNode,// Parent DOM node
  3. parentSelection = d3.select(parentNode),// Selection containing the parent DOM node
  4. circle = parentSelection.select('circle'); // selection containing a circle under the parent selection

在回调中,您可以使用:

  1. d3.select('text.my-class')
  2. .on('mouSEOver',function(d) {
  3. // The 'this' context is set to the DOM element,not the selection
  4. var circle = d3.select(this.parentNode).select('circle');
  5. circle.attr('fill','red');
  6. });

问候,

猜你在找的jQuery相关文章