How to access the parentNode of a d3.js selection?

前端 未结 4 1247
情歌与酒
情歌与酒 2020-12-05 06:46

I\'ve created the following document:


    ​
    <         


        
4条回答
  •  清歌不尽
    2020-12-05 07:18

    A D3 selection is just a double-array wrapped around the element(s) selected. As you found with p3, you can dereference the arrays to find your first node, if you want. However, a better method does exist:

    From the docs for selection.node():

    Returns the first non-null element in the current selection. If the selection is empty, returns null.

    In your case:

    var dad = current_gene_pcp.node().parentNode;
    

    However, if you don't need the line other than the DOM handle, you might as well just get that directly:

    // 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 + "]");
    

提交回复
热议问题