簡體   English   中英

在D3中選擇子節點的正確方法

[英]Proper way to select child nodes in D3

我創建了一個帶有一些節點的SVG元素:

gnodes = svg.selectAll("g.node")
    .data(_nodes);   
var newNodes = gnodes.enter().append("g")
     .attr("class", "node")
     .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; }) 
     .call(drag)
     .on('mouseover', onMouseOver)
     .on('mouseout', onMouseOut);

newNodes.append("circle")
    .attr("cx", 0)
    .attr("cy", 0)
    .attr("r", radius);

newNodes.append("image")
    .attr("xlink:href", getImage)
    .attr("x", -radius/2)
    .attr("y", -radius/2)
    .attr("width", radius + "px")
    .attr("height", radius + "px");

在onMouseOver中我想更改突出顯示的圓圈的顏色,但我無法從收到的數據中獲取此項目:

function onMouseOver(d, i) {
   var c1 = d.select("circle"); // error
   var c2 = i.select("circle"); // error
   var c3 = d.selectAll("circle"); // error
   var c4 = i.selectAll("circle"); // error 
}

使用d3獲取子節點的方法是什么?

d是數據對象, i是索引。 兩者都不是提供對任何d3 select功能的訪問的d3實例。
試試這個:

myelement.on('mouseenter', function(d,i) {
    d3.select(this).select('circle');
});

暫無
暫無

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

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