2013-02-07 13 views
20

tworzę elementy SVG pośrednictwem javascript, i to działa dobrze, ale gdy tworzę element SVG tekst i określić jego zawartość, przeglądarka po prostu nie czynią wartość, mimo wartość jest w kodzie, gdy sprawdzam go za pomocą firebuga.Ustawienie elementu SVG tekst dynamicznie poprzez javascript

Kod jest:

var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg'); 
svg.setAttribute('xlink','http://www.w3.org/1999/xlink'); 
svg.setAttribute('width','187'); 
svg.setAttribute('height','234'); 

var rect = document.createElementNS('http://www.w3.org/2000/svg', 'rect'); 
rect.setAttribute('width','187'); 
rect.setAttribute('height','234'); 
rect.setAttribute('fill','#fff'); 
rect.setAttribute('stroke','#000'); 
rect.setAttribute('stroke-width','2'); 
rect.setAttribute('rx','7'); 

var text = document.createElementNS('ttp://www.w3.org/2000/svg', 'text'); 
text.setAttribute('x', '10'); 
text.setAttribute('y', '20'); 
text.setAttribute('fill', '#000'); 
text.textContent = '2'; 

svg.appendChild(rect); 
svg.appendChild(text); 

var wp = document.getElementById('wrapper'); 
wp.appendChild(svg); 

Oto jsfiddle Link http://jsfiddle.net/sAhyC/ Jeśli skontrolować SVG pojawi się wartość elementu tam tekstu, ale przeglądarka nie czyni go.

Dzięki

+0

Zgaduję, że "dynamicznie" nie jest tutaj problemem. –

Odpowiedz

11

jesteś krótko o "h" w przestrzeni nazw

oceniły

var text = document.createElementNS('ttp://www.w3.org/2000/svg', 'text'); 

powinny być

var text = document.createElementNS('http://www.w3.org/2000/svg', 'text'); 
+0

Tak! Zgadza się! Czytałem ten kod niezliczoną ilość razy, ale tego nie zauważyłem. Bardzo ci dziękuję –

6
function createText() { 

    var newText = document.createElementNS(svgNS,"text"); 
    newText.setAttributeNS(null,"x",20);  
    newText.setAttributeNS(null,"y",100); 
    var textNode = document.createTextNode("milind morey"); 
    newText.appendChild(textNode); 
    document.getElementById("firstGroup").appendChild(newText); 
} 

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="300" height="300">  
     <g id="firstGroup"> 

    <text x="20" y="45" onclick="createText();" font-size=+13px">Click on this text to create a new text.</text> 

    </g> 
     </svg>