D3.js d3文本仅在添加到组时显示。为什么?

D3.js d3文本仅在添加到组时显示。为什么?,d3.js,D3.js,我在d3中的地图上添加了一些文字,下面是我的代码: countriesGroup = svg .append("g") .attr("id", "map"); //add background countriesGroup.append('rect') .attr("x", 0) .attr("y", 0) .attr("width", w) .attr("height",

我在d3中的地图上添加了一些文字,下面是我的代码:

    countriesGroup = svg
        .append("g")
        .attr("id", "map");

    //add background
    countriesGroup.append('rect')
        .attr("x", 0)
        .attr("y", 0)
        .attr("width", w)
        .attr("height", h);

    //Draw countries
    countries = countriesGroup.selectAll("path")
        .data(j.features)
        .enter()
        ...Draw country borders

    //Create Timer Text
    countriesGroup.append("text") <-----This worked
        .attr("transform", "translate(" + w/2 + "," + h/2 + ")")
        .style("fill", "#ff0000")
        .text("This is a test");

    //Create Timer Text
    svg.append("text") <-----This didn't
        .attr("transform", "translate(" + w/2 + "," + h/2 + ")")
        .style("fill", "#ff0000")
        .text("This is a test");
countriesGroup=svg
.附加(“g”)
.attr(“id”、“地图”);
//添加背景
countriesGroup.append('rect')
.attr(“x”,0)
.attr(“y”,0)
.attr(“宽度”,w)
.attr(“高度”,h);
//吸引国家
countries=countriesGroup。选择All(“路径”)
.数据(j.特征)
.输入()
…划定国家边界
//创建计时器文本

countriesGroup.append(“text”)您可以演示如何创建/选择svg,以及文本是否显示在DOM中(但可能在某些内容后面)?因为将文本附加到svg应该可以工作:如果直接将文本附加到svg,则需要指定x和y坐标,以绘制预期显示的文本(附加到countriesGroup可确保在该元素的坐标处绘制文本)。