svg innerHTML in firefox can not display

前端 未结 1 531
孤独总比滥情好
孤独总比滥情好 2021-01-21 04:59

i use innerHTML to add svg element,it works fine in chrome but in firefox it can not display; thanks so much for any ansower

    
    

        
1条回答
  •  迷失自我
    2021-01-21 05:11

    A workaround is to add the innerHTML code as HTML, and not SVG. You can do that simply by using a

    (instead of ) in your HTML code as the placeholder, and insert the full SVG via innerHTML.

    Replace:

    
    
    

    with

    And wrap your innerHTML string within an element:

    var svg = document.getElementById('container');
    svg.innerHTML   = ''
                    + ''
                    + 'hello world'
                    + ''
                    + '';
    

    This should work in both Chrome and Firefox. Here's a JSFiddle.

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