retrieve child elements of svg g

2019-08-01 17:50发布

I'm trying to retrieve the value of svg g element. Below is my code

<div id="chart">
    <svg width="1386" height="186">
    <g transform="translate(0,0)">    
    <g><circle class="node highlight" r="17.603478526018918" style="fill: #008000;" cx="521.2058097619438" cy="67.43023189750437"></circle>
    </g>
    <g><text class="label highlight" dx="22.603478526018918" dy=".35em" transform="translate(521.2058097619438,67.43023189750437)">CNN</text>
    </g>
    </g>
    </svg>
</div>

I want to access the third 'g' element value i.e. 'CNN' in above case. How can I do it using jQuery.

标签: jquery svg
2条回答
我想做一个坏孩纸
2楼-- · 2019-08-01 18:29

Try this:

console.log($('#chart').find('g').eq(2).text());

or this way:

console.log($('#chart').find('text').eq(0).text());

even a better one:

$('#chart g').find('text').text();
查看更多
来,给爷笑一个
3楼-- · 2019-08-01 18:34
s = '<div id="chart"><svg width="1386" height="186"><g transform="translate(0,0)"><g><circle class="node highlight" r="17.603478526018918" style="fill: #008000;" cx="521.2058097619438" cy="67.43023189750437"></circle></g><g><text class="label highlight" dx="22.603478526018918" dy=".35em" transform="translate(521.2058097619438,67.43023189750437)">CNN</text></g></g></svg></div>'

$('g:nth-of-type(2)',$(s))
查看更多
登录 后发表回答