如何使用javascript在svg元素(圆圈)中添加类?

时间:2017-05-03 03:06:10

标签: javascript svg

我正在尝试将类添加到svg元素(圆圈)但是,我得到了" [对象SVGAnimatedString]"当我试图在控制台上显示类名时作为输出。整个代码可用于小提琴:jsfiddle.net/prated/rsv0yxgx

<script>
  var counter =0;
  function selectForPValues()
  {

    var colorFormed = new Array("orange","green","blue","yellow","black","pink");
    var index = document.getElementsByClassName("selected").length;
    for(var i=0;i<index;i++)
     {
       textVar = document.getElementsByClassName("selected")[i].id;
       console.log("Id: "+ textVar);
       document.getElementById(textVar).className = "tester";
       document.getElementById(textVar).style.fill = colorFormed[counter];
       console.log("Resultant: "+ document.getElementById(textVar).className);
     }
    counter++;
  }
</script>

1 个答案:

答案 0 :(得分:0)

你不能用className向svg添加类,试试

svg.setAttribute("class", "class Name");