0
我目前有通过穿过(因此i
和j
)循环和附加<polygon>
如下面的(其嵌套在<svg>
内部)产生的六角形网格:D3自定义属性不被选择
d3.select("g").append("polygon").attr("points", grid[i][j]).attr("style", "fill:#fff;stroke:#000;stroke-width:1;").attr("posX", j).attr("posY", i);
,使得每个<polygon>
具有posX
和posY
的自定义属性。
当我尝试选择一个给定的六边形,并试图“亮点”的位置,如下面:
d3.select("polygon[posX='1'][posY='1']").attr("style", "fill:#999;stroke:#000;stroke-width:1;");
它根本不工作,不引发错误。如有必要,我可以发布更多代码。
这里是更广泛的点击功能:
$("polygon").click(function() {
var x = d3.select(this).attr("posX");
var y = d3.select(this).attr("posY");
d3.select(this).attr("style", "fill:#333;stroke:#000;stroke-width:1;");
$("#cords").text(x + "," + y);
//neighbors
for (var i = 0; i < 6; i++) {
var ret = getNeighbor("ooq", i, x, y);
console.log(ret);
//d3.select("polygon[posX='" + ret[0] + "']").attr("style", "fill:#111;stroke:#000;stroke-width:1;");
d3.select("polygon[posX='1'][posY='1']").attr("style", "fill:#999;stroke:#000;stroke-width:1;");
}
});
我试图与这些解决方案都无济于事:
- D3 Equivalent to jQuery Attribute selector
- d3.select by attribute value
- Is it possible to set custom attributes of SVG objects as number and not as string?
如果我错过了其中一个参考文献中的重要观点,请告诉我。