我正在使用 D3 V4 ,并且我正在尝试获取被点击的对象的ID。哎呀,我很想看到任何返回的属性值(d,fill,class等)。相反,无论属性如何,我都会返回null。代码如下。
compileSdkVersion
答案 0 :(得分:2)
您确定要附加点击处理程序的所选元素是否具有填充,类或ID?
尝试将点击处理程序添加到路径中。
d3.json("0267_02_combine.json", function(json) {
var features = json.features;
var path = d3.geoPath()
.projection(projection.fitExtent([[0, 0], [w, h]], json));
var path = svg.selectAll(".room")
.data(json)
.enter().append("path")
.attr("class", "room")
.attr("d", path)
.attr("fill","lightblue")
.attr("id", function(d){
return d.properties.loc;
});
path.on("click", function() {
console.log(d3.select(this)); //I see stuff! Yay!
console.log(d3.select(this).attr("fill")); //returns null
console.log(d3.select(this).attr("id")); //returns null
console.log(d3.select(this).attr("class")); //returns null
var coords = d3.mouse(this);
console.log(coords); //returns SVG coordinates
console.log(projection.invert(d3.mouse(this))); //returns lat/lon
})
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
&#13;