2013-10-02 12 views
5

Dynamicznie tworzę elementy SVG z JavaScript. Działa to dobrze dla obiektów wizualnych, takich jak prostokąt, ale mam problem z tworzeniem funkcjonujących xlinks. W poniższym przykładzie pierwszy prostokąt (który jest zdefiniowany statycznie) działa poprawnie po kliknięciu, ale dwa pozostałe (utworzone w JavaScript) ignorują kliknięcia ... mimo że sprawdzenie elementów w Chrome wydaje się mieć tę samą strukturę.Dynamiczne tworzenie odsyłaczy SVG w JavaScript

Widziałem wiele podobnych pytań, ale żaden nie dotyczy tego. Najbliżej znalazłem [adding image namespace in svg through JS still doesn't show me the picture], ale to nie działa (jak wspomniano poniżej). Moim celem jest zrobić to wyłącznie w JavaScript, bez zależności od JQuery lub innych bibliotek.


<!-- 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

Per komentarzu w link, próbowałem go setAttributeNS również. Wygląda jednak na to, że używałam niewłaściwej przestrzeni nazw (svg zamiast xlink). – user2837568

Odpowiedz

7

Tylko <a> może być link więc dodanie XLink: atrybut href do elementu <rect> będzie miał żadnego wpływu.

Musisz użyć setAttributeNS, który według ciebie nie działa, ale robi to dla mnie, więc być może był jakiś inny problem.

Ten przykład działa dla mnie:

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

Dzięki, że rzeczywiście rozwiązany. Wygląda na to, że problem polegał na tym, że kiedy wcześniej wypróbowałem createElementNS, używałem niewłaściwego obszaru nazw: http://www.w3.org/2000/svg zamiast http://www.w3.org/2000/svg. Moje (niepoprawne) myślenie polegało na tym, że element "a" jest częścią przestrzeni zwisu, gdy teraz widzę, że przestrzeń nazw powinna zostać określona na poziomie atrybutu (xlink). – user2837568

Powiązane problemy