2010-06-10 34 views
3

我使用下面的代碼來繪製一個SVG文本框,然後將它的文本定位屬性更改爲「左」,因爲它默認爲居中,這很討厭。使用JS將屬性應用於SVG提示'this.textBox.style未定義'。爲什麼?

文本生成正確,但是當我添加第二行時,在我的錯誤控制檯中出現錯誤'this.textBox.style is undefined'。

這裏是我的代碼:

RenderTextBox:function() 
{ 
    // Render Text 
    this.textBox = paper.text(this.x, this.y, this.htmlText); 
    this.textBox.style.textAnchor="left"; 
} 

任何想法?

回答

3

我想你想要做的是

this.textBox.setAttribute('text-anchor', 'start'); 

(或者,因爲它看起來像你使用拉斐爾)

this.textBox.attr('text-anchor', 'start'); 

text-anchor的有效值是startmiddleend ,和inherit

+1

完美!出於某種原因,我無法找到該屬性值的可靠列表。 – 2010-06-11 08:15:28

1

你可以使用括號表示來設置它:

this.textBox.style['text-anchor'] = "left"; 

這樣你實際上不必進行函數調用。

相關問題