简体   繁体   English

如何在forEach循环中选择元素? D3

[英]How to select elements in a forEach loop? D3

I am trying to modify an attribute in my links for a passed in Node object. 我正在尝试在链接中为传入的Node对象修改属性。

I am getting this error: 我收到此错误:

this.setAttribute is not a function

I am wondering what the restrictions are on the select operator. 我想知道对选择运算符有什么限制。 This is my current implementation: 这是我当前的实现:

function setChildrenLinkWidth(node) {
    node.links().forEach(function (i) {
        d3.select(i.source)
        .attr("stroke", "blue");
     });
 }

If anyone knows, I would appreciate it. 如果有人知道,我将不胜感激。

For reference, I have tried 供参考,我尝试了

d3.select("#" + i.source)

And I get the error: 我得到错误:

Failed to execute 'querySelector' on 'Document': '#[object Object]' is not a valid selector.

Edit: 编辑:

Here is some more code: 这是更多代码:

    // Creating nodes here
    let nodeEnter = node.enter().append('g')
    .attr('class', 'node')
    .attr("transform", function (d) {
        return "translate(" + source.x0 + "," + source.y0 + ")";
    })
    .on('click', click);
nodeEnter.append('circle')
.attr('class', 'node')

 // Functions

function setChildrenLinkWidth(node) {
node.links().forEach(function (i) {
    console.log(i.source);
    d3.select(i.source.id)
    attr("stroke", "blue");
 });
}

function click(d) {
    if (d.children) {
        d._children = d.children;
        d.children = null;
    } else {
        d.children = d._children;
        d._children = null;
    }
    update(d);
    setChildrenLinkWidth(d);
}

I think it is because that i.source is the node that is the source and not simply the string you likely think you're getting based on the data... Therefore if you do d3.select("#" + i.source.id); 我认为这是因为i.source是作为源的节点,而不仅仅是您可能认为基于数据获取的字符串...因此,如果执行d3.select("#" + i.source.id); it will select the element with that ID. 它将选择具有该ID的元素。 Unless your i.source for each links is an object. 除非您的每个链接的i.source是一个对象。

I can clarify more if you post your data structure. 如果您发布数据结构,我可以澄清更多。

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

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