我创建了一个SVG文件(数据/ humanTest.svg):如何获取SVG文件中的多边形的ID?
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 19.2.1, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 800 800" style="enable-background:new 0 0 800 800;" xml:space="preserve">
<polygon fill="yellow" id="forehead" stroke="blue" stroke-width="2" points="318.1,24.6 308.7,40.3 308.7,79.6 360.6,82 378.6,46.6 370,8.9 346.4,17.5 "/>
</svg>
,我想记录的ID“额头”当我将鼠标悬停在SVG元素,但是当我运行我的代码我得到空。
这里是我的D3代码:
var main_chart_svg = d3.select("#diagram")
.append("svg")
.attr({
"width": 800,
"height": 800
}).append('g');
d3.xml("data/humanTest.svg").mimeType("image/svg+xml").get(function (error, xml) {
if (error) throw error;
var svgNode = xml
.getElementsByTagName("svg")[0];
main_chart_svg.node().appendChild(svgNode);
var innerSVG = main_chart_svg.select("svg");
innerSVG.append("text")
.attr("dy", "1em")
.attr("dx", "1em")
.text("Test text");
innerSVG.on("mousemove", function (d) {
//console.log(innerSVG.id);
//d3.select("#diagram svg").selectAll("g")
console.log(d3.select(this).attr('id')); //doesnt work,
//gives me the svg id (Layer_1) not the id of the polygons within the svg tag
})
});
会很感激,如果任何人都可以请帮助我。
谢谢你的回答,但我只给了一个多边形作为例子。你的代码工作,但只适用于一个多边形,我如何修改它的多个多边形,以便它可以拾取鼠标悬停在其上的多边形? –