How to add SVG element from String to DOM

后端 未结 1 807
情歌与酒
情歌与酒 2021-01-23 01:43

I would like to add SVG which includes rect and use tags from a string to the DOM.

I doesn\'t seams to work the way I do.

1条回答
  •  一向
    一向 (楼主)
    2021-01-23 01:45

    The SVG element needs to be in the svg namespace i.e. have an xmlns attribute with the appropriate value...

    		var documentAsString =
    		'\
    			\
    				\
    					\
    						\
    						\
    					\
    				\
    			\
    		';
    		
    		var newDocument = (new DOMParser()).parseFromString(documentAsString, "text/xml");
    		var container = newDocument.getElementById("container");
    		var useContainer = document.getElementById('use-container');
    
    		useContainer.removeChild(useContainer.firstElementChild);
    		useContainer.appendChild(container.getElementsByTagName('g')[0]);
    	
    		
    			
    				
    				
    			
    		
    
    		
    		
    			
    		
    	

    0 讨论(0)
提交回复
热议问题