Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 单击D3树节点_Javascript_D3.js - Fatal编程技术网

Javascript 单击D3树节点

Javascript 单击D3树节点,javascript,d3.js,Javascript,D3.js,我目前有一个D3树布局,其中包含可以在运行时添加的节点,并在每个节点上添加文本。除了节点.on(…)函数如节点.on(“mousedown”…)之外,几乎所有功能都可以正常工作。问题是节点本身不响应单击,而附加的文本响应。例如,node.on(“mousedown”,…)是在单击附加文本时触发的,而不是实际的节点。任何指导都将不胜感激!核心函数的代码如下所示,它只接收一个JSON样式的对象和一个父id,然后将该JSON数据作为给定父对象的子对象插入到树中: function update(rec

我目前有一个D3树布局,其中包含可以在运行时添加的节点,并在每个节点上添加文本。除了
节点.on(…)
函数如
节点.on(“mousedown”…)
之外,几乎所有功能都可以正常工作。问题是节点本身不响应单击,而附加的文本响应。例如,
node.on(“mousedown”,…)
是在单击附加文本时触发的,而不是实际的节点。任何指导都将不胜感激!核心函数的代码如下所示,它只接收一个JSON样式的对象和一个父id,然后将该JSON数据作为给定父对象的子对象插入到树中:

function update(record_to_add, parent) {
    if (nodes.length >= 500) return clearInterval(timer);

    // Add a new node to a random parent.
    var n = {id: nodes.length, Username: record_to_add.Username},
            p = nodes[parent];
    if (p.children) p.children.push(n); else p.children = [n];
    nodes.push(n);

    // Recompute the layout and data join.
    node = node.data(tree.nodes(root), function(d) { return d.id; });
    link = link.data(tree.links(nodes), function(d) { return d.source.id + "-" + d.target.id; });

    // Add entering nodes in the parent’s old position.
    node.enter().append("circle", "g")
            .attr("class", "node")
            .attr("r", 10)
            .attr("cx", function(d) { return d.parent.px; })
            .attr("cy", function(d) { return d.parent.py; });

    // Add entering links in the parent’s old position.
    link.enter().insert("path", ".node")
            .attr("class", "link")
            .attr("d", function(d) {
                var o = {x: d.source.px, y: d.source.py};
                return diagonal({source: o, target: o});
            });
    node.enter().insert("text")
            .attr("x", function(d) { return (d.parent.px);})
            .attr("y", function(d) { return (d.parent.py);})
            .text(function(d) { return d.Username; });

    node.on("mousedown", function (d) {
        var g = d3.select(this); // The node
        // The class is used to remove the additional text later
        console.log("FOO");
    });

    node.on("mouseover", function (d) {
        var g = d3.select(this); // The node
        // The class is used to remove the additional text later
        var info = g.append('text')
                .classed('info', true)
                .attr('x', 20)
                .attr('y', 10)
                .text('More info');
    });

    // Transition nodes and links to their new positions.
    var t = svg.transition()
            .duration(duration);

    t.selectAll(".link")
            .attr("d", diagonal);

    t.selectAll(".node")
            .attr("cx", function(d) { return d.px = d.x; })
            .attr("cy", function(d) { return d.py = d.y; });

    t.selectAll("text")
            .style("fill-opacity", 1)
            .attr("x", function(d) { return d.px = d.x; })
            .attr("y", function(d) { return d.py = d.y; });
}

为避免文本元素妨碍事件捕获,可以尝试将文本元素配置为忽略指针事件:

svg text {
    pointer-events: none;
}
您也可以直接使用d3执行此操作:

textSelection
    .attr('pointer-events', 'none');

简单地切换添加节点和文本的顺序似乎可以解决问题。然而,我希望有一个更优雅的解释,所以我保持开放的问题。美丽,正是我所寻找的@user3707153只是一个建议:如果您决定使用CSS版本,我建议在实践中避免使用
svg text
选择器,因为svg中的所有文本元素都将失去被选择或单击的能力。仅对要禁用的文本元素使用更具体的类。