2011-08-01 4 views
4

私を示していない、私はSVGの元のサイズパラメータで画像を追加するJavascriptを通過しようとしています。はJSを通じて、SVG画像の名前空間を追加すると、静止画のスケーリングされたバージョンによってパラメータを取得して作業した後、画像

Firebugのは私の要素、およびすべての必要なパラメータを示しているが、最高の願いと私は通じ得ていないのです。

this.svg = document.getElementsByTagNameNS('http://www.w3.org/2000/svg','svg');  
var bild = document.createElementNS('http://www.w3.org/2000/svg','image'); 
var BildURL = this.image[0][0].getAttribute('xlink:href'); 
var imgX = new Image(); 
imgX.src = BildURL; 

bild.setAttribute("x","60"); 
bild.setAttribute("y","40"); 
bild.setAttribute("width",imgX.width); 
bild.setAttribute("height",imgX.height);  
bild.setAttribute("id","image12976"); 
bild.setAttribute("xlink:href",BildURL); 
this.svg[0].appendChild(bild); 

私がFirebugを調べると、その要素は完全に存在します。

<image id="image12976" x="60" y="40" width="300" height="430" xlink:href="img/Akira1.jpg"> 

私はマウスの上を移動しましたが、「投資モード」では画像の矩形が表示されますが、コンテンツは表示されません。

誰かがここで私が間違ったことを教えてもらえますか?

ご連絡いただきありがとうございます。

テイマー

答えて

-3

null名前空間で画像を作成してみてください:

var bild = document.createElementNS(null, 'image'); 
13

あなたが理由の詳細については、DOM 3 Coreを参照して、接頭辞を持つ属性でのgetAttribute/setAttributeメソッドを使用しないでください。

あなたは

getAttributeNS('http://www.w3.org/1999/xlink', 'href')

setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', your_url_here)を使用する場合はうまく動作します。

+0

まさにそれだった..... !!!!昨日私はそれを考え出した。 ありがとうございます。 – SmileMZ

+0

間違っています。 'getAttributeNS( 'http://www.w3.org/1999/xlink'、 'xlink:href')'は動作しません。 'getAttribute(" xlink:href ")'が動作します。 https://developer.mozilla.org/en-US/docs/DOM/element.getElementsByTagNameNS – Green

+0

@Green:例を参考にしてください。名前空間付きの属性と名前空間を認識しないDOMメソッドを混在させることはお勧めできません。 ErikDahlströ[email protected] –