我如何将id分配给svg元素?(对raphaeljs酒吧)

Muj*_*der 4 javascript svg raphael graphael

我如何在g.raphael条形图中为raphaeljs条指定id,以及如何在之后访问它们

var r = Raphael("holder", 600, 500);
var data=[1,3,4,5];
var chart = r.g.barchart(30, 30, 350, 250, [data], {stacked: true, type: "soft"});
for (var i = 0; i < chart.bars[0].length; i++) {
      var bar = chart.bars[0][i];
      if (bar.value >= 7) {
                bar.attr("fill", "#bf2f2f");
                bar.attr("stroke", "#bf2f2f");
                bar.attr("id","id-"+i);   //this doesn't work
                bar.id="id-"+i;           //this also doesn't work
                //applied as per raphaeljs documentatoion 
                //[http://raphaeljs.com/reference.html#Element.id][1]
                   }
        }
Run Code Online (Sandbox Code Playgroud)

Den*_*nis 5

尝试

bar[0].id = "id-"+i;
//or
bar.node.id = "id-"+i;
Run Code Online (Sandbox Code Playgroud)

bar是拉斐尔元素.它[0]node属性指向实际的DOM元素.