簡體   English   中英

如何在forEach循環中選擇元素? D3

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

我正在嘗試在鏈接中為傳入的Node對象修改屬性。

我收到此錯誤:

this.setAttribute is not a function

我想知道對選擇運算符有什么限制。 這是我當前的實現:

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

如果有人知道,我將不勝感激。

供參考,我嘗試了

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

我得到錯誤:

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

編輯:

這是更多代碼:

    // 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.source是作為源的節點,而不僅僅是您可能認為基於數據獲取的字符串...因此,如果執行d3.select("#" + i.source.id); 它將選擇具有該ID的元素。 除非您的每個鏈接的i.source是一個對象。

如果您發布數據結構,我可以澄清更多。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM