Work with elements from embed or object tag

后端 未结 1 1592
轮回少年
轮回少年 2020-12-09 22:32

I have an svg graphics inserted in the page with an embed or object tag:




        
                      
相关标签:
1条回答
  • 2020-12-09 23:30

    It will show up as a separate document, similar to an iframe. You can access it like this:

    var svg = document.getElementById('graphics').contentDocument
    

    Note that it is important to wait until the svg file is loaded; you might want to put your code in the object element’s onload event handler, like this:

    <object data="graphics.svg" type="image/svg+xml" id="graphics" />
    <script>
      document.getElementById('graphics').addEventListener('load',function(){
        var svg = document.getElementById('graphics').contentDocument
        // do stuff, call functions, etc.
      })
    </script>
    
    0 讨论(0)
提交回复
热议问题