2013-10-02 17 views
5

JavaScriptからSVG要素を動的に作成しています。長方形のような視覚的なオブジェクトに対してはうまくいきますが、機能するxlinkを生成するのに問題があります。下の例では、最初の四角形(静的に定義されている)はクリックされたときに正しく動作しますが、JavaScriptで作成された他の2つはクリックを無視します... Chromeの要素を調べても同じ構造を示すようです。JavaScriptでのSVGリンクの動的作成

私は、複数の同様の質問が出てきたことを見てきましたが、これにはまったく対処していません。私が見つけた最も近いものは[adding image namespace in svg through JS still doesn't show me the picture]でしたが、それはうまくいきません(下記のように)。私の目標は、JQueryや他のライブラリに依存することなく、純粋にJavaScriptでこれを行うことです。


<!-- Static - this rectangle draws and responds to click --> 
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" id="svgTag"> 
    <a xlink:href="page2.html" id="buttonTemplate"> 
     <rect x="20" y="20" width="200" height="50" fill="red" class="linkRect"/> 
    </a> 
</svg> 

<script> 
    var svgElement = document.getElementById ("svgTag"); 

    // Dynamic attempt #1 - draws but doesn't respond to clicks 
    var link = document.createElementNS("http://www.w3.org/2000/svg", "a"); // using http://www.w3.org/1999/xlink for NS prevents drawing 
    link.setAttribute ("xlink:href", "page2.html"); // no improvement with setAttributeNS 
    svgElement.appendChild(link); 

    var box = document.createElementNS("http://www.w3.org/2000/svg", "rect"); 
    box.setAttribute("x", 30); 
    box.setAttribute("y", 30); 
    box.setAttribute("width", 200); 
    box.setAttribute("height", 50); 
    box.setAttribute("fill", "blue"); 
    link.appendChild(box); 

    // Dynamic attempt #2 (also draws & doesn't respond) - per https://stackoverflow.com/questions/6893391 
    box = document.createElementNS("http://www.w3.org/2000/svg", "rect"); 
    box.setAttribute("x", 40); 
    box.setAttribute("y", 40); 
    box.setAttribute("width", 200); 
    box.setAttribute("height", 50); 
    box.setAttribute("fill", "green"); 
    box.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', "page2.html"); 
    svgElement.appendChild(box); 

+0

、私もsetAttributeNSでそれをしようとしました。しかし、問題のように見えますが、私は間違った名前空間(xlinkではなくsvg)を使用していました。 – user2837568

答えて

7

のみ<a>はそうXLINKを追加するリンクとすることができます:<rect>要素のhref属性には効果がありません。

あなたは動作しないと言うsetAttributeNSを使用する必要がありますが、それはおそらく他の問題がありました。

この例では、私の作品:

リンクにコメントパー

var svgElement = document.getElementById ("svgTag"); 
 

 
var link = document.createElementNS("http://www.w3.org/2000/svg", "a"); 
 
link.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', "page2.html"); 
 
svgElement.appendChild(link); 
 

 
var box = document.createElementNS("http://www.w3.org/2000/svg", "rect"); 
 
box.setAttribute("x", 30); 
 
box.setAttribute("y", 30); 
 
box.setAttribute("width", 200); 
 
box.setAttribute("height", 50); 
 
box.setAttribute("fill", "blue"); 
 
link.appendChild(box);
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" id="svgTag"> 
 
</svg>

+1

ありがとう、それは確かにそれを解決しました。問題は、以前私がcreateElementNSを試してみたとき、私は間違った名前空間を使用していたということでした。http://www.w3.org/2000/svgではなくhttp://www.w3.org/2000/svgです。私の考え方は、名前空間が属性レベル(xlink)で決定されているはずですが、 "a"要素がサグ名前空間の一部であるという考え方でした。 – user2837568

関連する問題