Javascript 如何从d3中的给定节点中选择svg元素?

Javascript 如何从d3中的给定节点中选择svg元素?,javascript,d3.js,svg,Javascript,D3.js,Svg,如何获取由此创建的svg的属性值 // SVG setup var svg = d3.select(vizNode).append('svg') .attr('width', width) .attr('height', height) .style('background', 'white') .append('g') .attr('transform', 'translate(' + width / 2 + ',' + height / 2 + ')

如何获取由此创建的svg的属性值

// SVG setup
var svg  = d3.select(vizNode).append('svg')
    .attr('width', width)
    .attr('height', height)
    .style('background', 'white')
    .append('g')
    .attr('transform', 'translate(' + width / 2 + ',' + height / 2 + ')');

// Text
svg.append('text')
    .datum(newValue)
    .attr('class', 'meter-center-text')
    .style('text-anchor', 'middle')
    .style('fill', mainColor)
    .text(function(d){
        return parseFloat(newValue);
    });
console.log("Done update");
我想从文本中获取基准值(因为我要增加它)

我想我想要的东西有点像

previousValue = d3.select(vizNode).select('svg').select('text').attr('darum');
但这不会带来任何回报。确实如此

previousValue = d3.select(vizNode).select('svg');

不返回任何内容,因此我一定是走错了方向。

返回数据的函数是
selection.datum()
,而不是
selection.attr('darum')
。我还假设darum是一个打字错误

示例如下:


谢谢我花了很长时间想知道为什么这在我的代码中不起作用,但在我记起在循环的第一次迭代中svg还不存在之前!没有比这更像小学生了:P
var value = d3.select('div').select('svg').select('text').datum();
console.log(value)