如何访问d3.js选择的parentNode?

nac*_*cab 39 javascript d3.js

我创建了以下文档:

<g>
    <path class=?"line" name=?"gene_1" stroke=?"#aec7e8" d=?"M10.47...">?</path>?
    <path class=?"line" name=?"gene_2" stroke=?"#aec7e8" d=?"M10.47...">?</path>?
    <path class=?"line" name=?"gene_3" stroke=?"#aec7e8" d=?"M10.47...">?</path>?
    ...
</g>
Run Code Online (Sandbox Code Playgroud)

当我将鼠标悬停在每条路径上时,我想将它最后添加到svg:g中,以便它显示在其他行的顶部,但我不知道如何正确选择parentNode:

function onmouseover(d, i){
    var current_gene_name = d3.select(this).attr("name"),
        current_gene_pcp = d3.select(".line[name=" + current_gene_name + "]");

    p1 = this.parentNode 

    p2 = current_gene_pcp[0].parentNode

    p3 = current_gene_pcp[0][0].parentNode

    //p.appendChild(this);
}
Run Code Online (Sandbox Code Playgroud)

p1有效,但我想确保"this"是.line,所以我更喜欢使用current_gene_pcp,但是p2 <html></html>作为父级返回,即使p3返回正确的<g></g>.最后一个版本似乎是一个等待发生的错误.有没有更好的办法?

Phr*_*ogz 45

D3选择只是围绕所选元素的双重数组.正如您所发现的那样p3,如果需要,可以取消引用数组以查找第一个节点.但是,确实存在更好的方法:

来自以下文档selection.node():

返回null当前选择中的第一个非元素.如果选择为空,则返回null.

在你的情况下:

var dad = current_gene_pcp.node().parentNode;
Run Code Online (Sandbox Code Playgroud)

但是,如果您不需要DOM句柄以外的行,您可以直接得到它:

// Search document-wide...
var dad = document.querySelector(".line[name=" + current_gene_name + "]");

// ...or only as a child of the current DOM element
var dad = this.querySelector(".line[name=" + current_gene_name + "]");
Run Code Online (Sandbox Code Playgroud)


mbo*_*ock 35

这是将鼠标悬停元素移动到前面的快速方法:

selection.on("mouseover", function() { this.parentNode.appendChild(this); });
Run Code Online (Sandbox Code Playgroud)

另请参阅d3-js组中的相关线程.


Nav*_*Nav 5

有两种方法可以访问它.
要么像这样使用第三个变量:someNode.attr("someAttrib", function(d, i, j) { console.log(d, i, j); });.在j包含您提供给父节点的数据.
或使用d3.select(this.parentNode).data()[0].id;.

一个例子:

<!DOCTYPE html>
<meta charset="utf-8">
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<div id="area", width="1000px" height="1000px"></div>
<script>
var GAP = 10, NODE_RADIUS = 14;

  var nodes = [
    {id: 1, prop: [{id: 1, something: 1}], abc: 1},
    {id: 2, prop: [{id: 1, something: 1}, {id: 2, something: 1}, {id: 3, something: 1}], abc: 2},
    {id: 3, prop: [{id: 1, something: 1}, {id: 2, something: 1}], abc: 3},
    {id: 4, prop: [{id: 1, something: 1}], abc: 4},
    {id: 5, prop: [{id: 1, something: 1}], abc: 5},
    {id: 6, prop: [], abc: 6}
  ];

var nodeLayer = d3.select("#area").selectAll(".node").data(nodes, function(d) {return d.id; });
var iNodes = nodeLayer.enter().append("svg").attr("class", "node");
    //append the black circle
    iNodes.append("circle")
                .style("fill", "black")
                .attr("r", NODE_RADIUS)
                .attr("cx", function(d) {return 10 + d.id * 10;})
                .attr("cy", 100);

    iNodes.append("g").selectAll(".prop").data(function(d) {return d.prop;}).enter()
            .append("text")
                .text(function(d,i,j){console.log("parent id="+j); return d3.select(this.parentNode).data()[0].id;})
                .attr("dx", 50)
                .attr("dy", 50)
                .style("font-size", "8px")
                .style("fill", d3.rgb(180,0,0))
                .style("text-anchor", "middle");

</script>
</body>
Run Code Online (Sandbox Code Playgroud)