我尝试向某些节点添加新的 SVG 元素。为此,要添加元素的节点必须通过包含在文本内容中的字符串找到,例如,找到"id0"
在<text>
标签内具有的任何节点。
这是我的 HTML 层次结构的示例:
<pre>
<svg>
<g>
<g>
<text> id3 </text>
<text> 73% </text>
<svg> ... </svg>
</g>
<g>
<svg> ... </svg>
</g>
<g>
<text> id0 </text>
<text> 11% </text>
<svg> ... </svg>
</g>
<g>
<text> id1 </text>
<text> 66% </text>
<svg> ... </svg>
</g>
<g>
<svg> ... </svg>
</g>
</g>
</svg>
</pre>
Run Code Online (Sandbox Code Playgroud)
我绝对不知道正确的解决方案,但我认为它是这样的:
d3.select('svg').select('g').selectAll('g').each(function (d, i) {})
.select('g').select('text').filter(function () {
return (d3.select(this).text() === 'id0')
})
.select(function () {
return this.parentElement;
})
.append('svg')
.attr('width', …
Run Code Online (Sandbox Code Playgroud)