简体   繁体   English

如何为树节点的所有后代提供一个类

[英]How to give a class for all descendants of a tree node

Using d3js, in my tree layout, I need to change some properties of a selected node (choix1) and also all its descendants, so I would like to give for all descendants a class (choix2)使用 d3js,在我的树布局中,我需要更改选定节点(choix1)及其所有后代的一些属性,因此我想为所有后代提供一个类(choix2)

  .on("click", function() {
    d3.select(this).classed("choix1", true); // selecting a node
    d3.select(".choix1").children.classed("choix2", true); // giving a class (choix2) for all descendants
  })

It's ok for the selected node, I expect a modification in the descendants properties, but the actual properties are the old ones (there is no modifocations).选定节点没问题,我希望在后代属性中进行修改,但实际属性是旧的(没有修改)。

You are mixing D3 API with DOM API methods which, obviously, will not work.您正在将 D3 API 与 DOM API 方法混合使用,这显然是行不通的。 Trying to access the .children property on the D3 selection returned by d3.select(".choix1") breaks because that property is part of the interface ParentNode which is implemented by the DOM's Element interface being a parent interface of your actual elements.尝试访问.children上返回的D3选择属性d3.select(".choix1")断裂,因为属性是接口的一部分ParentNode这是由DOM的执行Element接口为您的实际元素的父接口。

If you want to use the .classed() method to set the class on the child elements you have to wrap them into a D3 selection by first calling .selectAll(this.children) .如果您想使用.classed()方法在子元素上设置类,您必须首先调用.selectAll(this.children)将它们包装到 D3 选择中。 Your code thus becomes:您的代码因此变为:

.on("click", function() {
  d3.select(this).classed("choix1", true); 
  d3.selectAll(this.children)   // wrap the children in a D3 selection first...
    .classed("choix2", true);   // then call methods on that selection.
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM