[英]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.