2014-04-07 28 views
0

我在SVG內部有一個JavaScript(純粹,簡單的JS,沒有jQuery),我通過document.getElementById()獲取了<path>。現在我嘗試添加一個'主動'類,但我卡住了。在SVG中操作<path>的類

這是我(簡化)代碼至今:在-object的

var element = document.getElementById(myId); 
//A console.log(element) returns the <path>-Object, so I guess this is working. 

if (element) { 
    //Add a class 
} 

我可以得到當前類(ES):

console.log(element.className.baseVal); 

但我不能添加 '主動' 來這...我已經嘗試過幾種方法,例如

element.attr('class', className + ' active'); 

..返回

Uncaught TypeError: Object #<SVGPathElement> has no method 'attr' 

有沒有一種簡單的方法在空白JavaScript來做到這一點?我在這裏錯過了什麼嗎?

回答

2

使用setAttribute()爲JavaScript .attr()是jQuery函數。

+0

哦,我的!非常感謝! –

3

這樣做:

element.setAttribute('class', className + ' active'); 

你試圖做它在jQuery的語法