0
我正在使用D3 V4並試圖獲取單擊的對象的ID。哎呀,我很想看到任何返回的屬性值(d,fill,class等)。相反,無論屬性如何,我都會返回null。下面的代碼。D3單擊事件返回null屬性
d3.json("0267_02_combine.json", function(json) {
var features = json.features;
var path = d3.geoPath()
.projection(projection.fitExtent([[0, 0], [w, h]], json));
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;
});
svg.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
})
});
添加部分或全部0267_02_combine.json的請 – ksav