小编Gho*_*rus的帖子

如何通过文本内容查找 DOM 元素?

我尝试向某些节点添加新的 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)

javascript dom d3.js

5
推荐指数
2
解决办法
1120
查看次数

标签 统计

d3.js ×1

dom ×1

javascript ×1